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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.;

 

View solution in original post

3 REPLIES 3
ballardw
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.;

 

Terho
Obsidian | Level 7

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!

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 3 replies
  • 3278 views
  • 2 likes
  • 3 in conversation