DATA Step, Macro, Functions and more

Adding quotes to a Macro Variable

Reply
Occasional Contributor
Posts: 15

Adding quotes to a Macro Variable

[ Edited ]

Hello everyone,


If I have a macro variable and I would like to put quotation marks around it, is there a function to do this?


Thanks

 

Example:

Have: %let ppl= 001-001 001-002;

Want:  "001-001" "001-002"

 

 

Super User
Posts: 17,963

Re: Adding parenthesis to a Macro Variable

I think you mean quotes. 

 

Are you you creatimg this manually? How do you plan to use it? There are ways but not easily so sometimes easier to do depending on how you create macro variable or how you plan to use it. 

Occasional Contributor
Posts: 15

Re: Adding parenthesis to a Macro Variable

Thanks Reeza for your response. I am using it as follows:

 

proc sql;

          select distinct subject into Smiley Tonguepl separated by ' '

          from opt.sv

          where foldername = "Early Study Withdrawal";

quit;

 

proc print data=opt.sv(where=(subject = &ppl));
var subject folder svdtc;
run;

 

ERROR: WHERE clause operator requires compatible variables.

Occasional Contributor
Posts: 15

Re: Adding parenthesis to a Macro Variable

I dont know why my SQL code threw in a smiley face! Smiley Happy

Super User
Posts: 10,552

Re: Adding parenthesis to a Macro Variable

I think you may want something like

proc sql;
   select distinct quote(strip(subject)) into :ppl separated by ' '
   from opt.sv
   where foldername = "Early Study Withdrawal";
quit;

proc print data=opt.sv(where=(subject in( &ppl)));
   var subject folder svdtc;
run;

The strip is to remove trailing blanks within the quotes which might cause issues with the comparison

 

And you didn't want to test if subject was equal to a list of values but a member of the list.

Super User
Super User
Posts: 6,502

Re: Adding parenthesis to a Macro Variable

Note that STRIP() will also remove any existing leading spaces, which could cause values to not match.  It is safer to use TRIM() in this case.

Super User
Posts: 10,552

Re: Adding parenthesis to a Macro Variable

Absolutely. I tend to forget that because my data routinely gets leading spaces removed.
Occasional Contributor
Posts: 15

Re: Adding parenthesis to a Macro Variable

Thanks so much BallardW.....that was super! Really appreciate your help. Smiley Happy

Super User
Posts: 9,691

Re: Adding quotes to a Macro Variable

%let ppl= 001-001 001-002;
%let want="%sysfunc(tranwrd(%cmpres(&ppl),%str( ),%str(" ")))";

%put &want;
Occasional Contributor
Posts: 15

Re: Adding quotes to a Macro Variable

Thanks Xia for that very interesting method...I am going to have to figure out what each element is doing! Complicated but quite impressive! Thx again! Smiley Happy

Super User
Posts: 9,691

Re: Adding quotes to a Macro Variable

Here is an alternative way by PRXCHANGE() .

 

%let ppl= 001-001 001-002;
%let want=%sysfunc(prxchange(s/(\S+)/"\1"/,-1,&ppl));

%put &want;
Valued Guide
Posts: 2,175

Re: Adding quotes to a Macro Variable

Looks like you already have your answer, but I'd like to offer more features to support what you seem to seek.
1
The select into: clause provides a feature NOTRIM to maintain lead/trailing blanks - if you need those.
2
When selecting columns in a query, you can define an alternative format and here all those functions like trim/strip/quote etc become redundant if you use the $quote format like :

proc sql;
select distinct subject format= $quote200. into : pl separated by ', '
from opt.sv
where foldername = "Early Study Withdrawal"
;
quit;


That 200 length should accomodate a wide value of subject plus 2 for those quotation marks.
Beware that the $quote trims trailing blanks, but inside SAS data the comparison results would not be affected by that trim.

It is interesting to compare the quoting format and function
- One trims and the other does not.
Contributor
Posts: 26

Re: Adding quotes to a Macro Variable

Hi,

just a small suggestion , when comparing text variable making them into same case, this will help resolve mis-match due to case inconsistency in the data..

 

Hope that helps

 

Rgds,

Abhi

Ask a Question
Discussion stats
  • 12 replies
  • 467 views
  • 6 likes
  • 7 in conversation