Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of bradanelson
bradanelson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of labradorchik

ASKER

"var5" number value should come from the "var5" from the data1 data set. Am I doing something wrong here?
%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..
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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!
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!
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.