DATA Step, Macro, Functions and more

subset in data step

Reply
Occasional Contributor
Posts: 16

subset in data step


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?

Super User
Posts: 17,840

Re: subset in data step

Post your full code.

Occasional Contributor
Posts: 16

Re: subset in data step

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;

Super User
Posts: 10,500

Re: subset in data step

Slight improvement but depending on people may be usefule:

if upcase("&NAME_SELECTED") = 'ALL'

will handle ALL, All, aLL, alL, all and others.

Occasional Contributor
Posts: 16

Re: subset in data step

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.

Occasional Contributor
Posts: 16

Re: subset in data step

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;

Super User
Super User
Posts: 6,502

Re: subset in data step

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.

SAS Employee
Posts: 15

Re: subset in data step

%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;

N/A
Posts: 0

Re: subset in data step

Use If and else if instead of else and where

Ask a Question
Discussion stats
  • 8 replies
  • 246 views
  • 0 likes
  • 6 in conversation