Resolving Macro Variable while Retaining Quotes within Quotes

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Resolving Macro Variable while Retaining Quotes within Quotes

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!

 

 

 


Accepted Solutions
Solution
‎03-05-2018 10:20 AM
Super User
Super User
Posts: 8,127

Re: Resolving Macro Variable while Retaining Quotes within Quotes

[ Edited ]

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


All Replies
Super User
Super User
Posts: 9,599

Re: Resolving Macro Variable while Retaining Quotes within Quotes

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.

Occasional Contributor
Posts: 6

Re: Resolving Macro Variable while Retaining Quotes within Quotes

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.

Super User
Super User
Posts: 9,599

Re: Resolving Macro Variable while Retaining Quotes within Quotes

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.

Occasional Contributor
Posts: 6

Re: Resolving Macro Variable while Retaining Quotes within Quotes

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.

Solution
‎03-05-2018 10:20 AM
Super User
Super User
Posts: 8,127

Re: Resolving Macro Variable while Retaining Quotes within Quotes

[ Edited ]

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"
Occasional Contributor
Posts: 6

Re: Resolving Macro Variable while Retaining Quotes within Quotes

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 135 views
  • 0 likes
  • 3 in conversation