SAS Data Sets Manipulations

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.
labradorchikAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
bradanelsonConnect With a Mentor Commented:
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
 
labradorchikAuthor Commented:
"var5" number value should come from the "var5" from the data1 data set. Am I doing something wrong here?
0
 
d507201Database Marketing ConsultantCommented:
%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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
theartfuldazzlerConnect With a Mentor Commented:
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
 
labradorchikAuthor Commented:
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
 
labradorchikAuthor Commented:
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
 
labradorchikAuthor Commented:
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
 
bradanelsonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.