BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

In the below program how to find the value in the macro variable '&col" created by proc sql.     why this statement 

if index(&col, "A") eq 0 then do;

 in  the below data step giving me errors.   If I use the same data  step in the macro i am getting missing values for variable "A" .  Please suggest how to avoid "A"  variable values not to set missing. Thank you.


data one; input a $ b $ c; datalines; a b 1 c d 2 ; proc sql noprint; select distinct(name) into: col separated by "$" from sashelp.vcolumn where libname="WORK" and memname="ONE"; quit; data one; set one; if index(&col, "A") eq 0 then do; A=" "; end; run;
 


%macro col;
data one;
set one;
%if %index(&col, "A") eq 0 %then %do;
A=" ";
%end;
run;
%mend;
%col;

 

 

2 REPLIES 2
Tom
Super User Tom
Super User

The value of the macro variable just replaces the reference and the resulting program statement is then evaluated.

So you made COL have a value like A$B$C.

If you substitute that into your IF and %IF statements you get:

if index(A$B$C, "A") eq 0 then do;
%if %index(A$B$C, "A") eq 0 %then %do;

So the IF statement is not valid since you cannot have a variable name with dollar signs in it.  And your data step only has the variables A,B and C anyway.  If you want to treat that as a character constant then your need to enclose it in quotes.  But note that  you  get false positives if some other name contains A as part of the name.  So you should use the INDEXW() function instead.

if indexw("A$B$C", "A","$") eq 0 then do;
if indexw("&col", "A","$") eq 0 then do;

The macro %IF condition can never be true since your string, A$B$C does not contain the letter A with quotes around it. You could remove the quotes from around A, but then you might get false positives if some other name contains A as part of the name.

 

If you really want to search for the name A in the list of names you could use something like this instead.

%if %index($A$B$C$, $A$) eq 0 %then %do;
%if %index($&col$, $A$) eq 0 %then %do;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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