BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
akosh
Calcite | Level 5

I would like to use a macro to create many bat files that contain quoted strings of text in order to execute another program using the command prompt. For example, I want my bat file to consist of a single line with two quoted strings (one constant string and one string depending on macro variable, separated by an unquoted less than sign), such as  

 

"some constant text here" < "text here from macro variable";

 

I have tried various combinations of single and double quotation marks in the put statement in code similar to this:

 

%let variable_text = text that can change here;

data _null_;

file "filepath\test.bat";

put ' "some constant text here" <' "&variable_text";

run;

 

The issue is both resolving the macro variable and retaining the quotation marks. Based on the variations I'm trying, the macro variable does not resolve, or the macro variable does resolve but the quotation marks are not retained in the output bat file. I have looked at this article with no success: https://support.sas.com/resources/papers/proceedings15/2221-2015.pdf

 

Thanks in advance for your help!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You need to show what you want written to the output text file for your example.

 

If you are really using PUT to write the data then why not use a variable instead of a literal?

%let variable_text = text that can "change" here;

data _null_;
  file "filepath\test.bat"; 
  length variable_text $200 ;
  variable_text=symget('variable_text');
  put  '"some constant text here" < ' variable_text :$quote. ;
run;

Let's try it by writing to the SAS log.

132   %let variable_text = text that can "change" here;
133
134   data _null_;
135    * file "filepath\test.bat";
136     file log;
137     length variable_text $200 ;
138     variable_text=symget('variable_text');
139     put  '"some constant text here" < ' variable_text :$quote. ;
140   run;

"some constant text here" < "text that can ""change"" here"

 Or if you need to use goofy Unix escape characters for embedded quotes.

141   %let variable_text = text that can "change" here;
142
143   data _null_;
144    * file "filepath\test.bat";
145     file log;
146     length variable_text $200 ;
147     variable_text=tranwrd(symget('variable_text'),'"','\"');
148     put  '"some constant text here" < "' variable_text +(-1) '"' ;
149   run;

"some constant text here" < "text that can \"change\" here"

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

What is it exactly you are trying to do.  It seems to me that creating many batch files which do the same thing is not optimal.  There are many methods for avoiding this, the simplest being having a batch program which takes one parameter, you then call the batch file with the different parameter each time.  But you can likely simplify that even further in that if you have a process to run from the command line, just call it directly.  Without knowing what the process is it hard to suggest.

akosh
Calcite | Level 5

I'm doing some data analysis via another program. I would like to call that program up and feed into it a particular set of data (by referencing the data set in the bat file), and do this repeatedly for different data sets.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

What program?  Can that program access files directly, if so dump your data out to CSV, the other program reads it in, then either loops over the data or uses by group processing (which is a real power in SAS).  Splitting data up is never a good idea, you can see that from your process here, you then have multiple datafiles, multiple batch files, loops in the other process to use this setup.  Keep it simple, 1 data file, 1 process, 1 loop.

akosh
Calcite | Level 5

It's a very obscure program that can only be run using a control file that references where the data set is saved. Unfortunately splitting up the data is my only option.

Tom
Super User Tom
Super User

You need to show what you want written to the output text file for your example.

 

If you are really using PUT to write the data then why not use a variable instead of a literal?

%let variable_text = text that can "change" here;

data _null_;
  file "filepath\test.bat"; 
  length variable_text $200 ;
  variable_text=symget('variable_text');
  put  '"some constant text here" < ' variable_text :$quote. ;
run;

Let's try it by writing to the SAS log.

132   %let variable_text = text that can "change" here;
133
134   data _null_;
135    * file "filepath\test.bat";
136     file log;
137     length variable_text $200 ;
138     variable_text=symget('variable_text');
139     put  '"some constant text here" < ' variable_text :$quote. ;
140   run;

"some constant text here" < "text that can ""change"" here"

 Or if you need to use goofy Unix escape characters for embedded quotes.

141   %let variable_text = text that can "change" here;
142
143   data _null_;
144    * file "filepath\test.bat";
145     file log;
146     length variable_text $200 ;
147     variable_text=tranwrd(symget('variable_text'),'"','\"');
148     put  '"some constant text here" < "' variable_text +(-1) '"' ;
149   run;

"some constant text here" < "text that can \"change\" here"
akosh
Calcite | Level 5

Thank you Tom! $quote was the magic I needed!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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