Hello,
I would like to create a sas file using put statement and run into some issue with %let. Could some one take a look and see how I can fix it?
```
filename outfile "&pgmname..sas" lrecl=2000;
data _null_;
file outfile;
put 'proc datasets lib = work kill memtype = data nolist;';
put 'quit;';
put "%include 'setup.sas';";
put "%let studyid= &study. ;";
```
if I set study= "101", forsome reason, I can not show "%let studyid=101;" in my outfile, but just an " there.
What should I do in order to show my "%let ..."?
I can see that you know about single quotes as masking content from the macro parser. But you want both "%include" and single quotes as part of the PUT text. And @PaigeMiller 's suggestion of %nrstr shows how the macro developers created a macro function to address the issue.
But you can still use single quotes for the %include and double quotes for the single-quoted text, by dividing the elements of the text appropriately:
put '%include ' "'setup.sas';" ;
You need to mask % and & so they are not resolved as macro characters with macro meanings. They need to be interpreted by your program as plain text.
put "%nrstr(%let studyid= &study.) ;";
I can see that you know about single quotes as masking content from the macro parser. But you want both "%include" and single quotes as part of the PUT text. And @PaigeMiller 's suggestion of %nrstr shows how the macro developers created a macro function to address the issue.
But you can still use single quotes for the %include and double quotes for the single-quoted text, by dividing the elements of the text appropriately:
put '%include ' "'setup.sas';" ;
You can use single quotes to mask the macro triggers. Then you can use double quotes inside the single quotes when you need to have quote marks in your generated code, e.g.:
data _null_;
file log;
put 'proc datasets lib = work kill memtype = data nolist;' ;
put 'quit;' ;
put '%include "setup.sas";' ;
put '%let studyid= &study.;' ;
run ;
If you're interested in learning the macro language, you may want to consider developing this as a macro. SAS macros are another option for generating SAS code. But plenty of folks like to use PUT statements as you are doing. The macro approach for this code might look like:
%macro setup(study=) ;
%local studyid ; *maybe global, depending on what you want;
proc datasets lib = work kill memtype = data nolist;
quit;
%include "setup.sas" ;
%let studyid=&study ;
%*more stuff here;
%mend setup ;
Which value of STUDY to you want to assign to STUDY_ID?
The value that exists when the data step with the PUT statements runs? If so then that is almost what you are currently doing. Just don't use double quotes around the %LET so that the macro processor will not try to interpret it before the DATA step runs.
put '%let studyid=' "&study.;";
Or the value that exists AFTER the %INCLUDE statement runs? If the latter then use single quotes so that the PUT statement will write out &STUDY instead of the value of STUDY.
put '%let studyid= &study. ;';
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.