Desktop productivity for business analysts and programmers

Conversion of list of numbers into list of text numbers

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Conversion of list of numbers into list of text numbers

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


Accepted Solutions
Solution
‎03-11-2016 11:35 AM
Super User
Super User
Posts: 7,694

Re: Conversion of list of numbers into list of text numbers

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


All Replies
Super User
Posts: 11,107

Re: Conversion of list of numbers into list of text numbers

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;
Solution
‎03-11-2016 11:35 AM
Super User
Super User
Posts: 7,694

Re: Conversion of list of numbers into list of text numbers

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

 

Occasional Contributor
Posts: 17

Re: Conversion of list of numbers into list of text numbers

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!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 568 views
  • 2 likes
  • 3 in conversation