BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

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 ..."?

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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';" ;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.) ;";

 

 

--
Paige Miller
mkeintz
PROC Star

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';" ;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Quentin
Super User

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 ;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Tom
Super User Tom
Super User

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. ;';

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 878 views
  • 3 likes
  • 5 in conversation