BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

1 ACCEPTED SOLUTION

Accepted Solutions
slchen
Lapis Lazuli | Level 10


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;

View solution in original post

4 REPLIES 4
slchen
Lapis Lazuli | Level 10


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;

stat_sas
Ammonite | Level 13

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;

Tom
Super User Tom
Super User

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;

hhchenfx
Barite | Level 11

Thanks Everyone for helping me.

Have a nice weekend.

HHC

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
  • 4 replies
  • 589 views
  • 6 likes
  • 4 in conversation