🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pyrite | Level 9

SUM OF in WHERE

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Re: SUM OF in WHERE

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;``````
3 REPLIES 3
Super User

Re: SUM OF in WHERE

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;``````
Pyrite | Level 9

Re: SUM OF in WHERE

Thanks for the considerate details. I didn't know whether WHERE uses SQL to subset.
Super User

Re: SUM OF in WHERE

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.

Discussion stats
• 3 replies
• 600 views
• 0 likes
• 2 in conversation