Solved

SAS Data Sets Manipulations

Posted on 2010-08-18
8
1,075 Views
Last Modified: 2013-11-16
I am trying to use external MACRO throughout my program to get a number value from "var1" to inside of one of my file names "file_&var1.DAT":

Note:
1.  "var1" is not in the dataset "data1"
2.  "var1" may be a number from 01 to 99

%let var1=%SYSGET (var5);
%MACRO START;

data _null_;
file 'dirname:file_&var1.DAT';
 set lib.data1
  put @1 var2 $char2.
        @3 var3 $char2.
        @5 var4 $char3.
        @8 var5 $char2.
        @10 var6 $char1.;
run;
     
%MEND START;
%START;

Please let me know what am I doing wrong here?
I am getting the following WARNING: The argument to macro function %SYSGET is not defined as a system variable.
0
Comment
Question by:labradorchik
8 Comments
 
LVL 9

Accepted Solution

by:
bradanelson earned 250 total points
ID: 33469814
Where is var5 coming from in the following statement?
%let var1=%SYSGET (var5);


Once you have the GLOBAL VARIABLE var1 set, here is how to use it in the filename.  Notice the double quotes and the extra period I placed after the GLOBAL VARIABLE name.

%MACRO START;

data _null_;
file "dirname:file_&var1..DAT";
 set lib.data1
  put @1 var2 $char2.
        @3 var3 $char2.
        @5 var4 $char3.
        @8 var5 $char2.
        @10 var6 $char1.;
run;
     
%MEND START;
%START;
0
 

Author Comment

by:labradorchik
ID: 33469946
"var5" number value should come from the "var5" from the data1 data set. Am I doing something wrong here?
0
 
LVL 7

Expert Comment

by:d507201
ID: 33470042
%sysget is used to access environment variables like sasroot.  I don't think you want to have it here unless you're using the -sysparms statement when SAS session initializes.  

You're not trying to use the SYMGET function to retrieve a macro variable, are you?  If so, you don't neet it.  If var5 is already a macro variable then you need to use this syntax:
  %let var1=&var5;

The macro processor resolves &var5 and assigns the text value to var1.  The symget function is used in a DATA step and resolves at execution time, not compile time, so it's very handy.

If you work with macros, you really should get Art Carpenter's book "Carpenters Complete Guide to the SAS Macro Language."  All SAS professionals should have a copy, I think..
0
 
LVL 11

Assisted Solution

by:theartfuldazzler
theartfuldazzler earned 250 total points
ID: 33472833
Hi

Try the CALL SYMPUT function.

DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

%PUT *** &Var1 ***;

This takes the value of Var5 in the dataset Data1 (from the last record) and assigns it to the macro variable Var1
     
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:labradorchik
ID: 33473916
Thanks guys, but none of the above code is currently working for me...
I tried   %let var1=&var5; instead of %let var1=%SYSGET (var5);

than I tried
DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

%PUT *** &Var1 ***;

In both cases I am getting the following message: WARNING: Apparent symbolic reference VAR5 not resolved.
I thought I referenced "var5" in my data step's PUT statement?!? Am I doing it incorrectly?

I can see my output file "file_.DAT" now, but this file should appear in my directory as "file_50.DAT".
Note: if "var5"=50 for example
0
 

Author Closing Comment

by:labradorchik
ID: 33474169
I am sorry, below code is actually working fine, - I can see now file with the "var5" value embedded in it.
Thanks!!!

DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

Also,
"dirname:file_&var1..DAT";   is the correct format!
Thanks!
0
 

Author Comment

by:labradorchik
ID: 33479001
Only one thing is not working:

My output file "file_&var1.DAT" only contains the last record' value from "var1", - all other values are missed or not recorded.

Here is what I am trying to get:
For every different value of "var1" in the "data" SAS Dataset I am trying to output file "file_&var1.DAT.
So, if SAS dataset "data1" has total of 5 records and values for "var1" are for example 09, 09, 67, 78, 98, than I should have total of 4 "file_&var1.DAT" files as my output:
file_09.DAT
file_67.DAT
file_78.DAT
file_98.DAT

Right now for my output file I am only getting  file_98.DAT, which is my last record' value, which in this case is 98.
How come all other values for "var1" are not recorded and output files are not made?
Thanks!
0
 
LVL 9

Expert Comment

by:bradanelson
ID: 33479050
You will have to create a loop to process the mulitple output datasets needed.  1st, you read the file in to find out how many variations there are of var5 and then loop through a process to create a new dataset for each distinct variation.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
This article describes some very basic things about SQL Server filegroups.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now