Proc Expand with condition

Reply
Super Contributor
Posts: 371

Proc Expand with condition

Hi Everyone,

I run the below code to get the Highest value of current record and the previous 2 records for each "object".

I want to get the HIghest only when variable "time">=3. It can be done with the last step but I wonder if there is any way to build this condition in the Proc expand?

such as if time>=3 then do.

Thank you for your help.

HHC

input object time value1 value2;
datalines;
1 1 2 6
1 2 8 9
1 3 6 8
1 4 9 62
2 1 7 6
2 2 5 9
2 3 3 9
2 4 2 6
2 5 6 1
2 6 9 2
3 1 5 9
3 2 6 96
3 3 63 5
3 4 5 6
;
run;

proc expand data=have out=want method=none;
by object;
convert value1=max_value1/transformout=(movmax 3);
run;

data want; set want;

if time<3 then max_value1=.;

run;

Grand Advisor
Posts: 9,596

Re: Proc Expand with condition

data step can do that.

data have;
input object time value1 value2;
datalines;
1 1 2 6
1 2 8 9
1 3 6 8
1 4 9 62
2 1 7 6 
2 2 5 9
2 3 3 9
2 4 2 6
2 5 6 1
2 6 9 2
3 1 5 9 
3 2 6 96
3 3 63 5
3 4 5 6
;
run;
 

data want;
 set have;
 array x{0:2} _temporary_;
 x{mod(_n_,3)}=value1;
 if time gt 2 then max_value1=max(of x{*});
run;

Xia Keshan

Super Contributor
Posts: 371

Re: Proc Expand with condition

Hi Xia,

Can you explain the use of array in this code?

I find it diffifult to follow the logic.

Thank you.

HHC

Grand Advisor
Posts: 9,596

Re: Proc Expand with condition

Maybe you could follow the PG's code .

Respected Advisor
Posts: 4,609

Re: Proc Expand with condition

I think you could use:

proc expand data=have out=want2 method=none;

by object;

id time;

convert value1=max_value1/transformout=(movmax 3 trimleft 2);

run;

PG

PG
Ask a Question
Discussion stats
  • 4 replies
  • 206 views
  • 0 likes
  • 3 in conversation