BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dooheekim01
Obsidian | Level 7

The question is:

    • 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'.

The answer is:

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

 

 

I don't understand since there is no 'then do' or anything after if upcase(group)in ('A', 'B'). Could you explain why? Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The IF without a then is called a subsetting IF. It means "only keep records if the condition is true".

 

BTW that is not actually a correct answer. There is nothing that makes the values upper case to keep.

If the values are lower case they will still be so in the output.

data junk;
  input group $;
datalines;
a
b
c
d
e
g
junk
;

data want;
   set junk;
   if upcase(group) in ('A','B');
run;

Note that group = "a" and "b" are in the output.

there should be a statement like:

   group = upcase(group);

if the values in the output set are supposed to be upper case.

View solution in original post

9 REPLIES 9
Rick_SAS
SAS Super FREQ

This is called the "subsetting IF statement". It does not use a THEN clause. Think of it like a filter: Only observations that satisfy the IF condition are output to the data set.

PaigeMiller
Diamond | Level 26

@dooheekim01 wrote:

The question is:

    • 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'.

The answer is:

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

 

 

I don't understand since there is no 'then do' or anything after if upcase(group)in ('A', 'B'). Could you explain why? Thanks. 


The code would work, except that it does not do this part of the question: "convert all group values to upper case", so the output data set could contain lowercase 'a' or lowercase 'b'. You need one more statement to make all the values uppercase.

--
Paige Miller
ballardw
Super User

The IF without a then is called a subsetting IF. It means "only keep records if the condition is true".

 

BTW that is not actually a correct answer. There is nothing that makes the values upper case to keep.

If the values are lower case they will still be so in the output.

data junk;
  input group $;
datalines;
a
b
c
d
e
g
junk
;

data want;
   set junk;
   if upcase(group) in ('A','B');
run;

Note that group = "a" and "b" are in the output.

there should be a statement like:

   group = upcase(group);

if the values in the output set are supposed to be upper case.

dooheekim01
Obsidian | Level 7

Thank you so much!

wwwz
Calcite | Level 5

I have another question for the upcase function and the in operator. 

I used:

Upcase(group) in (upcase('a'), upcase('b'));

 

But it does NOT work, showing error message.

Please help.

Thanks

 

 

Rick_SAS
SAS Super FREQ

As stated in the documentation for the IN operator, the list of values on the right side must be CONSTANT values, not expressions.

wwwz
Calcite | Level 5

Thanks, I see.

anming
Pyrite | Level 9

the similar omissions can be found in the following cases;

 

proc sort data = scoredata_p;
by stu_id;
run;

proc sort data = scoredata;
by stu_id;
run;

data MMA;
merge scoredata_p(in = A) scoredata (in = B);
by stu_id;
if A and B;
run;

in selected variable merge (stu_id), you can omit A=1 and B=1 in 'if' statement.

   

TS4
Calcite | Level 5 TS4
Calcite | Level 5

why it gives different answer if i use where statement instead of if statement.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1772 views
  • 8 likes
  • 7 in conversation