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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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