BookmarkSubscribeRSS Feed
CurtisMackWSIPP
Lapis Lazuli | Level 10

The value is always false because you are always comparing "X" to "2" from dataset X.  You aren't accessing anything from dataset Y.

CurtisMackWSIPP
Lapis Lazuli | Level 10

I suspect you could solve this with a custom made format.

Aman4SAS
Obsidian | Level 7
Same is working , diff is not proc sql to create macro varible;
Code:
%GLOBAL FLAG;
%MACRO TEST();
%LET VAR=NAME;
%LET SEX=M;
%LET COND= =;
%IF &VAR &COND %BQUOTE('&SEX') %THEN %LET FLAG=Y;
%ELSE %LET FLAG=N;
%MEND;
DATA NEW;
SET SASHELP.CLASS;
CALL EXECUTE('TEST();');
FLAG=CALL SYMGET('FLAG');
RUN;

Its working fine.

Plz help
PaigeMiller
Diamond | Level 26

You can't refer to DATA set variables like NAME using a macro variable &VAR inside a macro. When you use &VAR to refer to a data set variable, you must use it inside a SAS data step. If you want to compare a data set variable to some other value, it must be done in a DATA step, using a data step IF statement and not a macro %IF statement, which only compares macro variables to a value.

 

So nothing you are trying to do with this macro will work.

 

You could, however, create code like this, that works inside a DATA step

 

%LET VAR=NAME;
%LET SEX=M;
%LET COND= =;

data want;
    set sashelp.class;
    if &var &cond "&sex" then flag='Y';
run;

Of course, your logic still fails, but the code produces no errors. Why does the code work now? Because &VAR is replaced with NAME and &COND is replaced by = and &SEX is replaced by M, yielding the line of code if name="M" then flag='Y'; inside a DATA step, which is indeed valid SAS code.

 

The logic fails because there are no observations in SASHELP.CLASS for which the variable NAME is equal to the value of "M". But I leave that up to you to fix.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Adding to my reply above

 

Here's a macro that does what you want

 

%macro subset(dsn=,var=,cond=,value=);
data want;
    set &dsn;
    if &var &cond "&value" then flag='Y';
run;
%mend;

/* Example: */
%subset(dsn=sashelp.class,var=name,cond=%str(=),value=Jane)

/* Example 2: */
%subset(dsn=sashelp.class,var=name,cond=%str(=:),value=J)

Works when &var references a data set character variable. Note that the comparison of the data step variable is done in a data step IF statement and not in a macro %IF statement, which cannot compare data step variables to a value.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 19 replies
  • 4715 views
  • 1 like
  • 4 in conversation