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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

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);

thesasuser
Pyrite | Level 9

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;

r_behata
Barite | Level 11
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;
novinosrin
Tourmaline | Level 20
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;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 696 views
  • 1 like
  • 5 in conversation