Is it impossible to use SUM OF in WHERE? For example, suppose six dummy variables as follows.
data have;
do i=1 to 30;
a1=ranbin(1,1,0.5);
a2=ranbin(1,1,0.5);
a3=ranbin(1,1,0.5);
b1=ranbin(1,1,0.5);
b2=ranbin(1,1,0.5);
b3=ranbin(1,1,0.5);
output;
end;
run;
The following successfully subsets by IF with SUM OF.
data usual;
set have;
if sum(of a: b:)=3;
run;
However, it seems WHERE does not allow SUM OF, while allows SUM per se.
data want;
*set have(where=(sum(of a: b:)=3));
set have;
*where sum(of a: b:)=3;
*where sum(of a1--b3)=3;
where sum(a1,a2,a3,b1,b2,b3)=3;
run;
Should I always list all the variables to use WHERE? There are too many variables to be specified. Thanks.
Subsetting if uses pure data step syntax, "where" uses SQL syntax, where the "of" construct is not available.
You can automate it by retrieving the variable list from dictionary.columns into a macro variable:
proc sql noprint;
select name into :varlist separated by ',' from dictionary.columns
where
libname = 'WORK' and memname = 'HAVE' and
upcase(substr(name,1,1)) in ('A','B')
;
quit;
data want;
set have;
where sum(&varlist.) = 3;
run;
Subsetting if uses pure data step syntax, "where" uses SQL syntax, where the "of" construct is not available.
You can automate it by retrieving the variable list from dictionary.columns into a macro variable:
proc sql noprint;
select name into :varlist separated by ',' from dictionary.columns
where
libname = 'WORK' and memname = 'HAVE' and
upcase(substr(name,1,1)) in ('A','B')
;
quit;
data want;
set have;
where sum(&varlist.) = 3;
run;
where statements and where= dataset conditions work not only in data steps, but in procedure steps also. The execution of where is done by the dataset engine, and the best common denominator for table access is to use SQL, so that's what SAS does.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.