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
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;
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;
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;
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: