Eren
Eren

Reputation: 101

How to construct SAS Macro variables with single quotes

I have a macro variable:

%let names = name1, name2, name3, name4 

The real variable can vary in length. What I want to do is make a new variable datalist, which looks like this:

‘name1’,‘name2’,‘name3’,‘name4’

I have tried to make this variable in the following way:

%str(%’)%sysfunc(tranwrd(%quote(&names.),%str(,),%str(%’ ,%’)))%str(%’))

When I run the code I get the following error:

The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended.

Adding white spaces does not help though. Does anyone know a different method to construct my desired macro variable?

PS: I have seen the following question, but in that one there were no commas separating the elements in the list. SAS macro variable quotes

Upvotes: 0

Views: 2159

Answers (3)

rshdev
rshdev

Reputation: 383

I was not able to get the other approaches to work. This abomination did:

%global quoted_list;
%let request_status = item1+item2+item3;

%macro parselist(s);
  data work.tmplist;
    %do i = 1 %to %sysfunc(countw(&s,"+"));
      %let word = %scan(&s,&i,"+");
      x="&word";
      output;
    %end;
  run;

  proc sql NOprint;
    select quote(trim(x),"'") into :quoted_list separated by ', ' from tmplist;
  quit;
%mend;

%macro main();
  %if "X&request_status." ne "X" %then %do;
    %parselist(&request_status.);
    %put &quoted_list;
  %end;
%mend;
%main;

Upvotes: 0

Tom
Tom

Reputation: 51566

Use the same method used before, but take care to hide the commas in NAMES from %SYSFUNC().

%let names = name1, name2, name3, name4 ;
%let qnames = %sysfunc(tranwrd(%bquote('&names'),%str(, ),', '));

Upvotes: 1

data _null_
data _null_

Reputation: 9109

38         proc sql noprint;
39            select quote(strip(name),"'") into :namelist separated by ', ' from sashelp.class;
40            quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

41         
42         %put NOTE: &=namelist;
NOTE: NAMELIST='Alice', 'Barbara', 'Carol', 'Jane', 'Janet', 'Joyce', 'Judy', 'Louise', 'Mary', 'Alfred', 'Henry', 'James', 
'Jeffrey', 'John', 'Philip', 'Robert', 'Ronald', 'Thomas', 'William'

Upvotes: 0

Related Questions