Automatic variable creation in Array

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Automatic variable creation in Array

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;


Accepted Solutions
Solution
‎06-13-2014 04:59 PM
Super Contributor
Posts: 275

Re: Automatic variable creation in Array


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


All Replies
Solution
‎06-13-2014 04:59 PM
Super Contributor
Posts: 275

Re: Automatic variable creation in Array


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;

Trusted Advisor
Posts: 1,204

Re: Automatic variable creation in Array

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;

Super User
Super User
Posts: 6,499

Re: Automatic variable creation in Array

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;

Super Contributor
Posts: 371

Re: Automatic variable creation in Array

Thanks Everyone for helping me.

Have a nice weekend.

HHC

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 204 views
  • 6 likes
  • 4 in conversation