BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasuser_01
Fluorite | Level 6

 

Refer to table listed all the way below. Am reading values of column name : data and it has all kinds of special characters in it and am storing it in macro variable name: chklist. I was able to see the values getting stored in chklist  successfully in  logs. Am getting error while trying to call macro variable in data step when am trying to use it in if condition. Any suggestions on how to resolve it..

 

proc sql noprint;
select data into :chklist separated by ','
from outdata.dnt_update_b08;
quit;
%let chklist1=%superq(chklist); 

%put &chklist1;


data ibs_test2;
set ibs_test;

if cmf_name in (&chklist1.) then do;
FLAG ='Y';
end;
output;
run;

 

Error:

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, iterator, (, ), ',', :.
Table

 

keydata
A(10/19
A****FRAUD
A***CLOSED
A**ATTN
A*_______________________
A%    STUDENT  NAME
A% LOFT
A% FRANK
A%PUBLIX
A%UNIVERSAL
A? DISNEY
A'___________________
A"__________________

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Issue 1: In the code shared the macro variable you populate is not the macro variable you use (&chklist vs. &chklist1)

Patrick_0-1675649198761.png

 

Issue 2: The text strings need quoting.

Your in operator needs something like: 

if cmf_name in ('string 1', 'string2') then do;

The value in the macro variable you populate is missing the quotes and looks like:

string 1,string 2

Change your SQL to also quote the strings.

Patrick_1-1675649526484.png

 

Issue 3: Use of macro quoted macro variable with in operator

Not all functions and operators deal (ignore) macro quoted strings. Either don't quote your macro variable or then unquote it for use with the in operator. With your current code and a quoted macro var this would need to look like:

Patrick_2-1675649731214.png

 

Below some working sample code as template for you.

proc sql noprint;
  select cats("'",name,"'") into :chklist separated by ','
    from sashelp.class(obs=2);
quit;
%put %nrbquote(&chklist);

data demo;
  set sashelp.class;
  if name in (&chklist);
run;


And last but not least: For the rare case where you need to preserve leading blanks use catt() instead of cats().

Patrick_0-1675650343938.png

 

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Whenever you get an ERROR or WARNING, post the complete (all code and messages) log of the failing step by copy/pasting it into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

 

All individual characters within an IN list must be contained in quotes. If the value contains single quotes, use double quotes around it, and vice versa. You can use a CASE clause in your SQL to do this conditionally.

Tom
Super User Tom
Super User

How would you write the IF statement without any macro variables?

Probably something like this.

if cmf_name in ('value1' 'value2') then do;

So create the macro variable so that it has similar strings.  If you are worried about macro triggers like & or % in the strings then make sure to use single quotes instead of double quotes.

select distinct quote(trim(data),"'") into :chklist separated by ' '
  from outdata.dnt_update_b08
;

Notice also:

There is no need to add commas between the values.  SAS is happy to let you use spaces instead.  That way you don't have to fight to copy the value of the macro variable in places where commas might have special meaning.

There is no need to include the trailing spaces that SAS stores in character variables.  SAS string comparison ignore the trailing spaces.

There is no need to add macro quoting to the macro variable.  When the strings are quoted with single quotes the macro processor will ignore the content of the strings.

There is a 64K byte limit to the length of a macro variable. So make sure that there are not too many values of DATA (or that the values are not too long).

Patrick
Opal | Level 21

Issue 1: In the code shared the macro variable you populate is not the macro variable you use (&chklist vs. &chklist1)

Patrick_0-1675649198761.png

 

Issue 2: The text strings need quoting.

Your in operator needs something like: 

if cmf_name in ('string 1', 'string2') then do;

The value in the macro variable you populate is missing the quotes and looks like:

string 1,string 2

Change your SQL to also quote the strings.

Patrick_1-1675649526484.png

 

Issue 3: Use of macro quoted macro variable with in operator

Not all functions and operators deal (ignore) macro quoted strings. Either don't quote your macro variable or then unquote it for use with the in operator. With your current code and a quoted macro var this would need to look like:

Patrick_2-1675649731214.png

 

Below some working sample code as template for you.

proc sql noprint;
  select cats("'",name,"'") into :chklist separated by ','
    from sashelp.class(obs=2);
quit;
%put %nrbquote(&chklist);

data demo;
  set sashelp.class;
  if name in (&chklist);
run;


And last but not least: For the rare case where you need to preserve leading blanks use catt() instead of cats().

Patrick_0-1675650343938.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 369 views
  • 0 likes
  • 4 in conversation