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

Hi, all, 

I am always marveled at your expertise and I do appreciate your help. 

 

My original dataset runs as follows: 

PERSONCUSIP6YEARDiff
3A19871
3A19881
3A19891
3A19967
3A19971
3A19981
12B19941
12B19951
12B19961
12B19971
13C20071
13C20081
13C20113
13C20121
13D19961
13D19971
13D19981

 

The by-group here is PERSON-CUSIP6. 

The very first observation by PERSON-CUSIP6 is always 1. 

The following observations can be 1's, or an integer larger than 1. But right after it, observations regain 1's. 

What I want to create is, if a value larger than 1 appears, I want to replace the subsequent values (1's) with that value (which is larger than 1) by each by group PERSON-CUSIP6. 

 

So, the results will look like this: 

 

PERSONCUSIP6YEARDiffDiff_k
3A198711
3A198811
3A198911
3A199677
3A199717
3A199817
12B199411
12B199511
12B199611
12B199711
13C200711
13C200811
13C201133
13C201213
13D199611
13D199711
13D199811

 

Thank you in advance! Wish you a wonderful weekend! 

 

KS ~ 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You ask a lot of questions today. Would you like to pay us ?

 

data have;
infile cards expandtabs truncover;
input PERSON	CUSIP6	$ YEAR	Diff;
cards;
3	A	1987	1
3	A	1988	1
3	A	1989	1
3	A	1996	7
3	A	1997	1
3	A	1998	1
12	B	1994	1
12	B	1995	1
12	B	1996	1
12	B	1997	1
13	C	2007	1
13	C	2008	1
13	C	2011	3
13	C	2012	1
13	D	1996	1
13	D	1997	1
13	D	1998	1
;

data want;
 set have;
 retain diff_k;
 by person cusip6;
 if first.cusip6 then diff_k=.;
 diff_k=max(diff_k,diff);
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User

You ask a lot of questions today. Would you like to pay us ?

 

data have;
infile cards expandtabs truncover;
input PERSON	CUSIP6	$ YEAR	Diff;
cards;
3	A	1987	1
3	A	1988	1
3	A	1989	1
3	A	1996	7
3	A	1997	1
3	A	1998	1
12	B	1994	1
12	B	1995	1
12	B	1996	1
12	B	1997	1
13	C	2007	1
13	C	2008	1
13	C	2011	3
13	C	2012	1
13	D	1996	1
13	D	1997	1
13	D	1998	1
;

data want;
 set have;
 retain diff_k;
 by person cusip6;
 if first.cusip6 then diff_k=.;
 diff_k=max(diff_k,diff);
run;
PaigeMiller
Diamond | Level 26

Hello, @KS99 

 

You already asked this question and received an answer that you marked correct. https://communities.sas.com/t5/SAS-Programming/How-to-replace-the-subsequent-rows-with-a-value-right...

 

Please don't ask the same question more than once.

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 833 views
  • 0 likes
  • 3 in conversation