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

Hello,

 

I've been trying to execute the following code:

 

%let line = '%put NOTE: foo;';

data _null_;
 infile "echo &line. >> /opt/testfolder/test_program.sas" pipe;
input;
put _infile_;
run;

However, when getting the results from the execution, the file I'm trying to append is not appended.

 

If I remove the "%" character from the first line, the command works just fine. 

 

How could I achieve the desired result (writing this piece of code to a file)? I've tried to use some functions such as str, nrstr, quote and nrquote and I wasn't able to execute it properly.

 

Thank you

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @bsas94,

 

I would rather omit the single quotes in the %LET statement and instead use the %NRSTR function to mask the percent sign and the semicolon:

%let line = %nrstr(%put NOTE: foo;);

 

Then you can use the %TSLIT macro (see Sample 25076: Resolve a macro variable within single quotation marks) in the INFILE statement to add quotes where needed. Apparently, you are on a Unix system, whereas my suggestions below were tested on a Windows workstation (using a different path, of course).

 

To append the SAS code %put NOTE: foo; without quotes to a text file:

infile %tslit(echo &line >> /opt/testfolder/test_program.sas) pipe;

 

To append '%put NOTE: foo;' including the single quotes to a text file:

infile %tslit(echo %tslit(&line) >> /opt/testfolder/test_program.sas) pipe;

 

 

 

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @bsas94,

 

I would rather omit the single quotes in the %LET statement and instead use the %NRSTR function to mask the percent sign and the semicolon:

%let line = %nrstr(%put NOTE: foo;);

 

Then you can use the %TSLIT macro (see Sample 25076: Resolve a macro variable within single quotation marks) in the INFILE statement to add quotes where needed. Apparently, you are on a Unix system, whereas my suggestions below were tested on a Windows workstation (using a different path, of course).

 

To append the SAS code %put NOTE: foo; without quotes to a text file:

infile %tslit(echo &line >> /opt/testfolder/test_program.sas) pipe;

 

To append '%put NOTE: foo;' including the single quotes to a text file:

infile %tslit(echo %tslit(&line) >> /opt/testfolder/test_program.sas) pipe;

 

 

 

Tom
Super User Tom
Super User

Huh?

Aren't you making this way too complicated?

data _null_;
  file "/opt/testfolder/test_program.sas" mod;
  put '%put NOTE: foo;';
run;

If you have the value in a macro variable use SYMGET() to get it into a character variable. 

data _null_;
  file "/opt/testfolder/test_program.sas" mod;
  length line $200;
  line = symget('line');
  put line ;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 1296 views
  • 0 likes
  • 3 in conversation