Hi Everyone,
My data is as below.
I want to create 3 variable New1 New2 New3 to store the value of the following comparison:
If var1>max_1 then New1=1; else New1=0;
If var1>max_2 then New2=1; else New2=0;
If var1>max_3 then New3=1; else New3=0;
I try to do it with Array. My problem is that I don't know how to tell SAS to create the Array of New1 New2 New3 automatically within the last section of the code.
Thank you for your help.
HHC
data have;
input var1 max_1 max_2 max_3;
datalines;
3 2 2 3
4 2 8 9
0 5 5 63
8 9 1 2
6 11 4 6
100 20 5 9
run;
data want; set have;
array max(3) max_3-max_3;
do i=1 to 3;
if var1>max then NEW_VARIABLE=1;
end;
run;
data want;
set have;
array max max_1-max_3;
array new new1-new3;
do i=1 to dim(max);
new(i)=ifn( var1>max(i),1,0);
end;
drop i;
run;
data want;
set have;
array max max_1-max_3;
array new new1-new3;
do i=1 to dim(max);
new(i)=ifn( var1>max(i),1,0);
end;
drop i;
run;
Try this one.
data have;
input var1 max_1 max_2 max_3;
datalines;
3 2 2 3
4 2 8 9
0 5 5 63
8 9 1 2
6 11 4 6
100 20 5 9
run;
data want(drop=i);
set have;
array current_max(3) max_1-max_3; /* Avoid using max as this is sas function name */
array NEW_VARIABLE(3) new1-new3; /* New array */
do i=1 to 3;
if var1>current_max then NEW_VARIABLE=1;
else NEW_VARIABLE=0;
end;
run;
proc print data=want;
run;
SAS creates variables when you reference them.
So you can use the ARRAY statement to "create" the variables.
Or a LENGTH statement.
Or any number of other statements.
Also note that SAS evaluates logical expressions to 1 (true) or 0 (false).
So here is simple program to do what you requested.
data want;
set have;
array max max_1-max_3;
array new new1-new3 ;
do over max ;
new = (var1 > max) ;
end;
run;
Thanks Everyone for helping me.
Have a nice weekend.
HHC
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.