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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 213 views
  • 0 likes
  • 3 in conversation