SAS Programming

DATA Step, Macro, Functions and more
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
Lapis Lazuli | Level 10

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1095 views
  • 1 like
  • 5 in conversation