BookmarkSubscribeRSS Feed
Learn_uk
Calcite | Level 5

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"

 

 

17 REPLIES 17
Reeza
Super User

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. 

Learn_uk
Calcite | Level 5

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

 

proc sql;

          select distinct subject into :ppl 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.

Learn_uk
Calcite | Level 5

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

ballardw
Super User

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.

Tom
Super User Tom
Super User

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.

ballardw
Super User
Absolutely. I tend to forget that because my data routinely gets leading spaces removed.
Learn_uk
Calcite | Level 5

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

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

%put &want;
Learn_uk
Calcite | Level 5

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! 🙂

Ksharp
Super User

Here is an alternative way by PRXCHANGE() .

 

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

%put &want;
Steelers_In_DC
Barite | Level 11

I'm a big fan of this solution, it helps  to use a macro variable date within teradata.

 

Excellent!

Peter_C
Rhodochrosite | Level 12
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.
AbhiD
Obsidian | Level 7

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

mansour_ib_sas
Pyrite | Level 9

Hello,

 

I have this solution, the result is correct but,  i have the error of evaluate.

Can someone help me understand his provenance?

Thank you

 

%macro tt();
%global f;

%let a=001-001 001-002;

%let f= %str( );

%do i=1 %to %sysfunc(countw(&a.,' '));

%let f=%sysfunc(catx(%str( ),&f.,"%scan(&a.,&i,' ')") );

%end;

%mend;
%tt;
%put =&f.;

///////

ERROR: %SYSEVALF function has no expression to evaluate.
         %put =&f.; ="001-001" "001-002"

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 17 replies
  • 24885 views
  • 9 likes
  • 10 in conversation