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

Hello, 
I have a doubt in the number of observations .

question : create a temporary data set, cleandata36.
In this data set, convert all group values to upper case.
Then keep only observations with group equal to 'A' or 'B'.

 

 

when I run  below code 


data cleandata36;
set cert.input36 ;
if upcase(group) in ('A','B');
run;

 

then number of observations are different 

 

but when I run 

data cleandata36;
set cert.input36 ;
group=upcase(group);

where group in ('A','B');
run;

 

the number of observations are different. 

but the logic I want to implement is maybe same. Can please anyone tell me that why there is mismatch of observations 🙂 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

The Where Statement is a compile-time statement. It controls what observations are being read into the data step. Meaning that in the code below, it considers the values read - NOT the upcased values. 

 

The Subsetting If Statement is considered at run-time. Therefore, in the code below, it considers the upcased values.

 

Hope this makes sense 🙂

 

data have;
input group :$1;
datalines;
A
a
B
b
; 

data test1;
   set have;
   group = upcase(group);
   where group in ('A', 'B');
run;

data test2;
   set have;
   group = upcase(group);
   if group in ('A', 'B');
run;

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

The WHERE statement is applied to the input dataset, and therefore to the raw values. To make it work, use the function in the statement:

where upcase(group) in ('A','B');
PeterClemmensen
Tourmaline | Level 20

The Where Statement is a compile-time statement. It controls what observations are being read into the data step. Meaning that in the code below, it considers the values read - NOT the upcased values. 

 

The Subsetting If Statement is considered at run-time. Therefore, in the code below, it considers the upcased values.

 

Hope this makes sense 🙂

 

data have;
input group :$1;
datalines;
A
a
B
b
; 

data test1;
   set have;
   group = upcase(group);
   where group in ('A', 'B');
run;

data test2;
   set have;
   group = upcase(group);
   if group in ('A', 'B');
run;

 

librasonali
Quartz | Level 8
so basically , whatever i will write above where ?? it won't take that condition in observation ?
Kurt_Bremser
Super User

The position of a WHERE statement in the code is irrelevant, as it is applied in the read engine for the incoming dataset. A WHERE statement has the same function that a WHERE= dataset option has, and in a certain sense you can consider it to work "outside" the data step.

I also recommend that you study the documentation of the WHERE Statement thoroughly.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 4 replies
  • 2345 views
  • 3 likes
  • 3 in conversation