Hello,
I want to convert a string of numbers inputted from a user prompt into a string of "text" numbers:
e.g.
%let qtr = 1, 2, 3, 4, 5;
into
'1', '2', '3', '4', '5'
Anyone have any tips on how to do this?
I have been using something like below for a single numeric value. However, not too sure of an easy way to do a list of numbers:
%let quote_qtr = %str(%')&qtr.%str(%');
Thanks
A datastep alternative can be found below. I would ask though what you intend doing with this macro variable later on. Remember macro has no datatypes other than text, and has no real data manipulation function, it is purely a text generation tool. If you want to do anything further with this data (or in fact I tend to do it this way anyways) I would suggest putting the "data" in a "dataset". True if you just want to do a in () command it may be fine, but if you want to do further computation, e.g if value is 3 or 4 then add 10, the macro logic will rapidly become verbose and confusing. Far simpler to first create a dataset:
%let qtr = 1, 2, 3, 4, 5; data parameters (drop=i); do i=1 to countw("&qtr.",","); parameter=scan("&qtr.",i,', '); output; end; run;
And then use that for future processing, for instance (also you can have as many as you want then):
proc sql; create table WANT as select * from HAVE where ID in (select PARAMETER from PARAMETERS); quit;
Datastep alternative.
%let qtr = 1, 2, 3, 4, 5; data _null_; length tmp $2000; do i=1 to countw("&qtr.",","); tmp=catx(',',tmp,quote(scan("&qtr.",i,', '))); end; call symputx('NEW_QTR',tmp); run; %put &NEW_QTR.;
Below is a utility macro that does what you've requested.
Note that because the commas inside your macro variable would normally be delimiters for multiple parameters you can't just use the raw macro variable. This is somewhat robust in that extra commas don't appear in the final result and an empty string remains empty.
If you start passing it strings with other than comma or space delimiter you would need to change the calls to countw and %scan to work.
The quotes are " not '. I hope that isn't a problem.
Note that your single quote approach can be changed to: %let quote_qtr = %sysfunc(quote(&qtr)); which is somewhat easier on the eyes.
%macro quotevals(values);
%let vstr=;
%do i = 1 %to %sysfunc(countw(&values));
%if &i=1 %then %let Vstr = %sysfunc(quote(%scan(&values,&i)));
%else %let vstr = &vstr,%sysfunc(quote(%scan(&values,&i)));
%end;
&vstr
%mend;
%let test= 1,24, 45,16;
%let qtest= %quotevals(%quote(&test));
%put &qtest;
A datastep alternative can be found below. I would ask though what you intend doing with this macro variable later on. Remember macro has no datatypes other than text, and has no real data manipulation function, it is purely a text generation tool. If you want to do anything further with this data (or in fact I tend to do it this way anyways) I would suggest putting the "data" in a "dataset". True if you just want to do a in () command it may be fine, but if you want to do further computation, e.g if value is 3 or 4 then add 10, the macro logic will rapidly become verbose and confusing. Far simpler to first create a dataset:
%let qtr = 1, 2, 3, 4, 5; data parameters (drop=i); do i=1 to countw("&qtr.",","); parameter=scan("&qtr.",i,', '); output; end; run;
And then use that for future processing, for instance (also you can have as many as you want then):
proc sql; create table WANT as select * from HAVE where ID in (select PARAMETER from PARAMETERS); quit;
Datastep alternative.
%let qtr = 1, 2, 3, 4, 5; data _null_; length tmp $2000; do i=1 to countw("&qtr.",","); tmp=catx(',',tmp,quote(scan("&qtr.",i,', '))); end; call symputx('NEW_QTR',tmp); run; %put &NEW_QTR.;
Thanks for your help!
Yes, I am using the "quoted" list for an in () statement for a "where" condition.
I thought the double quotes would be an issue, but it seems to be working. I am using your datastep alternative, as this seems a bit more simple and straightforward for my purposes.
Thank you both for the help!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.