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.
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.