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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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