if-then

Reply
Occasional Contributor
Posts: 7

if-then

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.

Super User
Posts: 8,062

Re: if-then

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 20,226

Re: if-then

[ Edited ]

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;

Ask a Question
Discussion stats
  • 2 replies
  • 80 views
  • 0 likes
  • 3 in conversation