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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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