The value is always false because you are always comparing "X" to "2" from dataset X. You aren't accessing anything from dataset Y.
I suspect you could solve this with a custom made format.
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.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.