Hi,
I hava a dataset like this:
data have;
input provision1 provision2 provision3 provision4 flow $20.;
datalines;
5 5 8 7 increase
0 0 4 4 inflow
0 2 3 3 inflow
8 8 8 0 outflow
;
run;
I'm trying to create a new variable (impact) and, if flow = "Inflow", I would like assign it the first provision greater then zero, in my example provision03 for the 2nd observation and provision2 for the third observation. In the other case provision will be missing.
I am trying with this code:
data test;
set have;
array prov {*} provision1 provision2 provision3 provision4;
if flow = "inflow" then do;
impact = 0;
do i = 1 to 4 until(prov{i} eq 0);
impact = prov{i+1};
end;
end;
run;
I think there is some problem because the value for impact in the new dataset are 0 (2nd obs) and 2 (3rd obs) and not 4 and 2 as I would like.
I hope I have explained my need well, could you help me please...? Thanks,
Daniele
data have;
input provision1 provision2 provision3 provision4 flow $20.;
datalines;
5 5 8 7 increase
0 0 4 4 inflow
0 2 3 3 inflow
8 8 8 0 outflow
;
run;
data want;
set have;
array t(*) provision4-provision1;
if flow='inflow' then do;
k=whichn(0,of t(*));
if k>1 then impact=t(k-1);
end;
drop k;
run;
I believe you need to change your until condition
from:
do i = 1 to 4 until(prov{i} eq 0);
to:
do i = 1 to 4 until(prov{i} ne 0);
The following will give the desired result.
data work.test (drop=i);
set work.have;
array prov {*} provision1 provision2 provision3 provision4;
if flow = "inflow" then do;
impact = 0;
do i = 1 to 3 ;
test=0;
if prov{i+1}>test then test=prov{i+1};
if test >0 then leave;
end;
output;
end;
run;
data test;
set have;
array prov {*} provision1 provision2 provision3 provision4;
if flow = "inflow" then do;
impact = 0;
do i = 1 to dim(prov) ;
if prov[i] > 0 then do ;
impact =prov[i];
leave;
end;
end;
end;
drop i;
run;
data have;
input provision1 provision2 provision3 provision4 flow $20.;
datalines;
5 5 8 7 increase
0 0 4 4 inflow
0 2 3 3 inflow
8 8 8 0 outflow
;
run;
data want;
set have;
array t(*) provision4-provision1;
if flow='inflow' then do;
k=whichn(0,of t(*));
if k>1 then impact=t(k-1);
end;
drop k;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: