BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello, 

 

I am having trouble with this basic if then statement. I am trying to sort the table into 4 different categories but when I run this piece of code the whole capacity_label column has 4s in it even though not all the capacity variables are <25. Do I need to change my syntax? 

 

Thank you. 

 

data in_capacity2;
set in_capacity;
if Capacity > 80 then Capacity_label=1;
if 50 >Capacity > 79 then Capacity_label=2;
if 25 >Capacity > 49 then Capacity_label=3;
if Capacity <25 then Capacity_label=4;
run;

4 REPLIES 4
Reeza
Super User

Is your variable spelled correctly?

Please post your log. 

 

Or try the following and see what happens. 

 

data in_capacity2;
set in_capacity;
if capacity = . then capacity_label = -99;
else if Capacity > 80 then Capacity_label=1;
else if 50 >Capacity > 79 then Capacity_label=2;
else if 25 >Capacity > 49 then Capacity_label=3;
else if Capacity <25 then Capacity_label=4;
run;

proc freq data=in_capacity2;
table capacity*capacity_label/missing;
run;

 

PaigeMiller
Diamond | Level 26

One problem in your code is the following:

 

if 50 >Capacity > 79 then Capacity_label=2;

Is it possible for 50 to be greater than Capacity and simultaneously Capacity to be greater than 79? Yes or no? Why or why not?

 

Without having your data, I would re-write the whole thing as:

 

data in_capacity2;
set in_capacity;
if capacity > 80 then Capacity_label=1;
else if capacity>50 then Capacity_label=2;
else if capacity>25 then Capacity_label=3;
else Capacity_label=4;
run;

If that doesn't work for you, then you need to show us (a portion of) your data.

--
Paige Miller
Tom
Super User Tom
Super User

The code can be improved, but it will only assign ALL of the observations a CAPACITY_LABEL value of 4 if the CAPACITY variable is always less than 25.  Note that includes any observations where the value of CAPACITY is missing because SAS considers all of the 28 possible missing values as less than actual number.

 

You should add ELSE's to your string of IF/THEN so it doesn't have to keep testing once it finds a match.

You should make sure the tests aren't tautologies.  There is no number that is both less than 50 and greater than 79.

Make sure to include the boundary values into one of the two adjacent categories.

data in_capacity2;
  set in_capacity;
  if Capacity > 80 then Capacity_label=1;
  else if 50 < Capacity <= 80 then Capacity_label=2;
  else if 25 < Capacity <= 50 then Capacity_label=3;
  else if missing(Capacity) then Capacity_label=.;
  else Capacity_label=4;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 644 views
  • 0 likes
  • 5 in conversation