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

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
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
Junyong
Pyrite | Level 9
Thanks for the considerate details. I didn't know whether WHERE uses SQL to subset.
Kurt_Bremser
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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