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

Hi Everyone,

 

I have just run into a problem with macro variable lists.

 

I have 2 data sets:

Capture1.PNG

 

Capture2.PNG

created with the following codes respectively:


data Subidiaries;
   length subs $25;
   input Subs $;
cards;
Walmart
Sainsbury
Argos
Holland
PureGym
;

 

data stats;
   length subs $25 Customer_count 8.;
   input subs $ customer_count;
cards;
Walmart 33
Argos 55
Pimps 66
Lugos 67
;

 

I create a macro variable list from the first table above use in filtering on the second table. The queries used are:

proc sql noprint;
select subs
into :List_subs separated by ' '
from Subidiaries;
quit;

 

data subset;
  set stats;
  where subs in (&List_subs);
 quit;

 

However, I get the below error in the log. Can anyone advice me on how to resolve this?

Capture3.PNG

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The data step IN wants to see quotes around each individual value

 in ('Walmart' 'Sainsbury' )

 

Change

proc sql noprint;
select subs
into :List_subs separated by ' '
from Subidiaries;
quit;

 

to

proc sql noprint;
select quote( strip(subs))
into :List_subs separated by ' '
from Subidiaries;
quit;

to place quotes around each value. The strip may be needed to preven having spaces depending on the actual values and declared length of the variable SUBS.

 

If you are using that list elsewhere where you do not want the quotes you'll need to build to separate list variables. 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

correction:

 


proc sql noprint;
select quote(trim(subs))
into :List_subs separated by ' '
from Subidiaries;
quit;

 

data subset;
set stats;
where subs in (&List_subs);
run;

ballardw
Super User

The data step IN wants to see quotes around each individual value

 in ('Walmart' 'Sainsbury' )

 

Change

proc sql noprint;
select subs
into :List_subs separated by ' '
from Subidiaries;
quit;

 

to

proc sql noprint;
select quote( strip(subs))
into :List_subs separated by ' '
from Subidiaries;
quit;

to place quotes around each value. The strip may be needed to preven having spaces depending on the actual values and declared length of the variable SUBS.

 

If you are using that list elsewhere where you do not want the quotes you'll need to build to separate list variables. 

Tom
Super User Tom
Super User

So you need quotes around string literals. So either add the quotes when making the macro variable.  You can use the QUOTE() function in your SQL query.

 

It is a common enough problem to convert a space delimited list of words into a list of quoted words that there are many macros available to make the conversion.  For example:  %qlist

 

But for simple list with just a single space between the words you can do it with a simple call to TRANWRD() function.

data subset;
  set stats;
  where subs in ("%sysfunc(tranwrd(&List_subs,%str( )," "))");
run;

 You can clean up a user entered list that might have multiple spaces between the words by using the COMPBL() function.

%let qlist=%sysfunc(compbl(&list_subs));
%let qlist="%sysfunc(tranwrd(&qlist,%str( )," "))";

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!

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.

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
  • 4324 views
  • 1 like
  • 4 in conversation