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

Hi All,

I met with a technical problem.Here is the sample list,and sort by account and month:

accontmonthRelPmtRemark
001201211RONo need output
001201212RPOutput
001201301RPOutput
001201302ROOutput
001201303TPNo need output
001201304TONo need output
001201305RONo need output
001201306RONo need output
001201307RPOutput
001201308ROOutput
002201302RONo need output
002201303RONo need output
002201304TPNo need output
002201305RPOutput
002201306TPNo need output
002201307RPOutput
002201308ROOutput
002201309ROOutput
002201310RPOutput
002201311RPOutput
003201305RONo need output
003201306TONo need output
003201307RPOutput
003201308TPNo need output
003201309RONo need output

The conditons of output the accouunt are:

1.output those Rel="R" and Pmt="P"

2.and output all the subsequent Obs which Rel="R" for the same account.Here subsequent means continuous.

I highlighted which needs to output at the column "Remark"..

I have found a way,and it took so many steps and It's hard to explain to my Boss,

Please help!

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Keith
Obsidian | Level 7

using RETAIN to keep the previous value is useful here to make the logic fairly simple.

data have;

input account month Rel $ Pmt $;

datalines;

1 201211 R O

1 201212 R P

1 201301 R P

1 201302 R O

1 201303 T P

1 201304 T O

1 201305 R O

1 201306 R O

1 201307 R P

1 201308 R O

2 201302 R O

2 201303 R O

2 201304 T P

2 201305 R P

2 201306 T P

2 201307 R P

2 201308 R O

2 201309 R O

2 201310 R P

2 201311 R P

3 201305 R O

3 201306 T O

3 201307 R P

3 201308 T P

3 201309 R O

;

run;

data want;

set have;

by account month;

retain flag;

if first.account then flag=0;

flag=(rel='R' and (pmt='P' or flag=1));

if flag=1 then do;

  drop flag;

  output;

end;

run;

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

I think that the BY statement option NOTSORTED will be helpful.

filename FT15F001 temp;
data accont;
   infile FT15F001 DSD DLM='09'X;
  
input accont Month $ (Rel Pmt)(:$1.) Remark &$16.;
  
parmcards;
001   201211   R  O  No need output
001   201212   R  P  Output
001   201301   R  P  Output
001   201302   R  O  Output
001   201303   T  P  No need output
001   201304   T  O  No need output
001   201305   R  O  No need output
001   201306   R  O  No need output
001   201307   R  P  Output
001   201308   R  O  Output
002   201302   R  O  No need output
002   201303   R  O  No need output
002   201304   T  P  No need output
002   201305   R  P  Output
002   201306   T  P  No need output
002   201307   R  P  Output
002   201308   R  O  Output
002   201309   R  O  Output
002   201310   R  P  Output
002   201311   R  P  Output
003   201305   R  O  No need output
003   201306   T  O  No need output
003   201307   R  P  Output
003   201308   T  P  No need output
003   201309   R  O  No need output
;;;;
   run;
proc print;
  
run;
data accont2;
   set accont;
   by accont rel pmt notsorted;
  
if first.accont then flag=0;
  
retain flag;
   if (rel eq 'R' and pmt eq 'P') then flag=1;
  
if flag then out=1;
  
if last.rel and (rel eq 'R') then flag=0;
  
run;
proc print;
  
by accont;
   id accont;
   run;
Keith
Obsidian | Level 7

using RETAIN to keep the previous value is useful here to make the logic fairly simple.

data have;

input account month Rel $ Pmt $;

datalines;

1 201211 R O

1 201212 R P

1 201301 R P

1 201302 R O

1 201303 T P

1 201304 T O

1 201305 R O

1 201306 R O

1 201307 R P

1 201308 R O

2 201302 R O

2 201303 R O

2 201304 T P

2 201305 R P

2 201306 T P

2 201307 R P

2 201308 R O

2 201309 R O

2 201310 R P

2 201311 R P

3 201305 R O

3 201306 T O

3 201307 R P

3 201308 T P

3 201309 R O

;

run;

data want;

set have;

by account month;

retain flag;

if first.account then flag=0;

flag=(rel='R' and (pmt='P' or flag=1));

if flag=1 then do;

  drop flag;

  output;

end;

run;

summy
Calcite | Level 5

Well,I was shocked,such clear and simple.Thanks Keith!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1027 views
  • 4 likes
  • 3 in conversation