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

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

1 ACCEPTED SOLUTION

Accepted Solutions
atul_desh
Quartz | Level 8

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;

View solution in original post

15 REPLIES 15
novinosrin
Tourmaline | Level 20

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?

atul_desh
Quartz | Level 8
Not exactly.... if Non-zero occurs and continue till end without getting zero then... its first occurrence -1......

2) if its value become zero again and then again non-zero value then it should chk then newest greater than 0 values ith-1...


a 0 b 2 c 4 d 0 ---> a
a 0 b 0 c 5 d 8 ---> b
a 0 b 3 c 0 d 9 ---> c
novinosrin
Tourmaline | Level 20

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;
atul_desh
Quartz | Level 8

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;

 

 

novinosrin
Tourmaline | Level 20

Ok, i think i understand better this time. Let me tweak

art297
Opal | Level 21

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

 

novinosrin
Tourmaline | Level 20

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;
art297
Opal | Level 21

@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

 

 

novinosrin
Tourmaline | Level 20

I agree

art297
Opal | Level 21

@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

 

novinosrin
Tourmaline | Level 20

Thank you @art297, I am wondering is it possible to get away from linear search? my brain isn't functioning well today

art297
Opal | Level 21

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

 

atul_desh
Quartz | Level 8

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;

novinosrin
Tourmaline | Level 20

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.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 15 replies
  • 1458 views
  • 1 like
  • 4 in conversation