BookmarkSubscribeRSS Feed
fama
Fluorite | Level 6

Hi all,

I am using if- then in SAS. I want to have some classifications like below

groups1: all variables from 1 to 7 should have the value under 7 (if they are not null), it means only observation 3 should be labelled as gropu1 (based on what is shown)

group2: all variables should be under 15( some can be under 7 some avove). this means it should be obs 1

gropu3: all variables above 15 which is obs4..

I know I might lose some observations (like obs 2)but it is not important

my goal is having the clear and right groups (1,2,3)

My code:

if var1<7 or

obs   var1  var2 var3 ....var7

1         2       4      2          13

2         3        1    17          2

3         2        4     6           5

4       16     21      22        18

 

 your help is appreciated.

2 REPLIES 2
Kurt_Bremser
Super User

Please post example data as a data step with datalines, for easy recreation.

Suggested code (untested, see above):

data want;
set have;
array vars{*} var1-var7;
group = 1;
group3 = 1;
do i = 1 to dim(vars);
  if vars{i} > 7 then group = max(group,2);
  if vars{i} <= 15 the group3 = 0;
end;
if group3 then group = 3;
drop group3;
run;
Reeza
Super User

Get the maximum and minimum value for each row - using the MAX/MIN function and then classify according to the max value.

data want; set have; 

array _mydata(*) var1-var4; 
x_max = max(of _mydata(*));
 x_min = min(of _mydata(*)); 
if x_min >= 15 then category='Group 3';
 else if x_max <= 7 then category = 'Group 1'; 
else if 7 < x_max < 15 then category = 'Group 2'; 
else category='Other';
 run; 


proc print;
 run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 378 views
  • 0 likes
  • 3 in conversation