hi, I have dataset, for example like this:
data NAME1;
infile datalines delimiter=",';
input name $ gender $ age;
datalines;
AB,F,1
CD,M,2
XX,F,2
OO,M,3.5
PP,F,2
AC,F,2
;
RUN;
I have a prompt value (NAMA_SELECTED) which user enter names. So if user enter ALL, all or leave it blank, I want to export all the records to the dataset SELECTED_PEOPLE. If user enter AB and/or CD only, these two records should be exported to SELECTED_PEOPLE dataset.
I use %_eg_whereParam for subsetting when user enter AB. and/or CD, not problem. But when it come to check if the prompt value is ALL, or all or blank, it just cannot output everything. I am using if "&NAMA_SELECTED" = 'ALL' or "&NAMA_SELECTED" = 'all' or "&NAMA_SELECTED" = ' ' then do; output;end;
What's wrong this logic?
Post your full code.
here is step for subsetting:
data selected_names;
set NAME1;
if "&NAME_SELECTED" = 'ALL' or "&NAME_SELECTED" = 'all' or "&NAME_SELECTED" = ' ' then do;
output;
end;
else do;
where %_eg_whereParam(name, NAME_SELECTED,IN,TYPE=S);
end;
run;
Slight improvement but depending on people may be usefule:
if upcase("&NAME_SELECTED") = 'ALL'
will handle ALL, All, aLL, alL, all and others.
does not work. From the log, the step took the if as where and did the where NAME = 'ALL' logic, which is similar to what I've seen before.
interesting, if I comment out the else do logic. just leave the first if end in the datastep. it export all the records.
data selected_names;
set NAME1;
if "&NAME_SELECTED" = 'ALL' or "&NAME_SELECTED" = 'all' or "&NAME_SELECTED" = ' ' then do;
output;
end;
else do; /*
where %_eg_whereParam(name, NAME_SELECTED,IN,TYPE=S);
OUTPUT;*/
end;
run;
WHERE statement is applied before the data is read into the data step. Placing it inside of a conditional does not change this.
Use IF statement instead if you want to apply the filter conditionally.
%macro filter;
data selected_names;
set NAME1;
%if %upcase(&NAME_SELECTED) ^= ALL and &NAME_SELECTED ne %then %do;
where %_eg_whereParam(name, NAME_SELECTED,IN,TYPE=S);
%end;
run;
%mend;
%filter;
Use If and else if instead of else and where
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.