I want to check "FC_2 -- FC_10" column and if FC_* >0 then corresponding "dt_*-1" should populate.
I'm able to do the above task by create code below.
but there is one another constrain if FC_* become zero once again in same row then need to check FC_*>0 once again.
Please help !!
data ds1;
input fc_1-fc_10 (dt_1-dt_10) (:$2.);
datalines;
7 0 8 6 5 0 7 0 0 0 a e a e e a a e a e
7 0 3 10 3 2 10 8 4 7 b d b d d b b d b d
3 0 0 0 2 7 4 4 0 9 f g f g g f f g f g
3 0 0 4 3 10 1 9 4 7 a d a d d a a d a d
;run;
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=2 to dim(fc);
if fc[i]>0 then do;
stp_dt=dt[i-1];
leave;
end;
end;
run;
/*output should be like for stp_dt*/
for_first_row = dt_6
for_second_row = dt_2
for_thrid_row = dt_9
for_fourth_row = dt_3
Thank you all 🙂
below was working fine !!
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until (fc(i)>0 and fc(i-1)=0) ;
if fc(i)>0 then stp_dt=dt[i-1];
end;
run;
Do you want to check the last element of the array that is greater than 0 and populate it's ith-1 value of dt array in stp_dt? Just seeking some clarification?
still not sure i comprehend you, see if this is close?
data ds1;
input fc_1-fc_10 (dt_1-dt_10) (:$2.);
datalines;
7 0 8 6 5 0 7 0 0 0 a e a e e a a e a e
7 0 3 10 3 2 10 8 4 7 b d b d d b b d b d
3 0 0 0 2 7 4 4 0 9 f g f g g f f g f g
3 0 0 4 3 10 1 9 4 7 a d a d d a a d a d
;run;
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until(fc(i)>0);
if fc(i)>0 then stp_dt=dt[i-1];
end;
run;
Thanks for reply, but not working as expected 😞
data ds1;
input fc_1-fc_10 (dt_1-dt_10) (:$2.);
datalines;
7 0 8 6 5 0 7 0 0 0 a b c d e f g h i j
7 0 3 10 3 2 10 8 4 7 a b c d e f g h i j
3 0 0 0 2 7 4 4 0 9 a b c d e f g h i j
3 0 0 4 3 10 1 9 4 7 a b c d e f g h i j
;run;
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until(fc(i)>0);
if fc(i)>0 then stp_dt=dt[i-1];
end;
run;
/**output**//
for_first_row = f;
for_second_row = b;
for_third_row = i;
for_fourth_row =b;
Ok, i think i understand better this time. Let me tweak
If your fourth value should really be c rather than b, then the following would work:
data ds1; input fc_1-fc_10 (dt_1-dt_10) (:$2.); datalines; 7 0 8 6 5 0 7 0 0 0 a b c d e f g h i j 7 0 3 10 3 2 10 8 4 7 a b c d e f g h i j 3 0 0 0 2 7 4 4 0 9 a b c d e f g h i j 3 0 0 4 3 10 1 9 4 7 a b c d e f g h i j ; data ds2; set ds1; array fc[*] fc_1-fc_10; array dt[*] dt_1-dt_10; do i=dim(fc) by -1 until(fc(i-1) eq 0 and fc(i)>0); if fc(i-1) eq 0 and fc(i)>0 then stp_dt=dt[i-1]; end; run;
Art, CEO, AnalystFinder.com
elder & wiser but never old at master and hall of famer @art297, Thank you for the lead as it requires a tiny adjustment to meet OP's requirement as I plagiarize yours 🙂
data ds1;
input fc_1-fc_10 (dt_1-dt_10) (:$2.);
datalines;
7 0 8 6 5 0 7 0 0 0 a b c d e f g h i j
7 0 3 10 3 2 10 8 4 7 a b c d e f g h i j
3 0 0 0 2 7 4 4 0 9 a b c d e f g h i j
3 0 0 4 3 10 1 9 4 7 a b c d e f g h i j
;run;
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until(fc(i-1) eq 0 and fc(i)>0);
if fc(i-1) eq 0 and fc(i)>0 then
do n=i-1 by -1 while(fc(n)=0);
stp_dt=dt(n);
end;
end;
drop i n;
run;
@novinosrin: You're assuming that @atul_desh didn't just make an error regarding what was desired for the 4th test case. That may or may not be the case.
If it is, your code as is won't correctly handle a case like:
0 0 0 4 3 10 1 9 4 7 a b c d e f g h i j
Art, CEO, AnalystFinder.com
I agree
@novinosrin: If your assumption is correct your code only needs one escape statement. e.g.:
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until(fc(i-1) eq 0 and fc(i)>0);
if fc(i-1) eq 0 and fc(i)>0 then
do n=i-1 by -1 while (fc(n) eq 0);
stp_dt=dt(n);
if n eq 1 then leave;
end;
end;
drop i n;
run;
Art, CEO, AnalystFinder.com
Thank you @art297, I am wondering is it possible to get away from linear search? my brain isn't functioning well today
Should be away to do it with a regular expression but, again, let's wait to see what @atul_desh really wants.
Art, CEO, AnalystFinder.com
Thank you all 🙂
below was working fine !!
data ds2;
set ds1;
array fc[*] fc_1-fc_10;
array dt[*] dt_1-dt_10;
do i=dim(fc) by -1 until (fc(i)>0 and fc(i-1)=0) ;
if fc(i)>0 then stp_dt=dt[i-1];
end;
run;
Thank you for the feedback. So @art297 solutions have always been slick and this one too with no surprises. Please mark torontonian's answer and close the thread. Thank you for the interesting question though.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.