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

Hi, I have a dataset where i have do last value carry forward by downword rows. 

Any leads would be helpful.

data CM;

input USUBJID       mmmol_injected;cards;

AUS-001001  520
AUS-001001  0
AUS-001001  .
AUS-001001  .

AUS-001001  .

AUS-001001  100
US-001001    0
US-001001    .

US-001001    100

US-001001    .

US-001001    100

US-001001    .

PAK-001001  70

PAK-001001  80

PAK-001001  0

PAK-001001  0

PAK-001001  .

PAK-001001  80

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. Try this

 

data CM;
input USUBJID $10. mmmol_injected;
cards;
AUS-001001  520
AUS-001001  0
AUS-001001  .
AUS-001001  .
AUS-001001  .
AUS-001001  100
US-001001    0
US-001001    .
US-001001    100
US-001001    .
US-001001    100
US-001001    .
PAK-001001  70
PAK-001001  80
PAK-001001  0
PAK-001001  0
PAK-001001  .
PAK-001001  80
;
run;

data want (drop=_:);
   set CM;
   by USUBJID notsorted;
   retain _mm;
   if first.USUBJID then _mm=.;
   if mmmol_injected ne . then _mm=mmmol_injected;
   else if mmmol_injected=. then mmmol_injected=_mm;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

You want the last non-missing value carried forward by USUBJID, correct?

sahoositaram555
Pyrite | Level 9
Sorry dear @PeterClemmensen,
it has a visitn column as well. So i have to do it by subjid & visitn .
data CM;

input USUBJID visitn mmmol_injected;cards;

AUS-001001 1 520
AUS-001001 2 0
AUS-001001 3 .
AUS-001001 4 .
AUS-001001 5 .
AUS-001001 6 100
US-001001 1 0
US-001001 2 .
US-001001 3 100
US-001001 4 .
US-001001 5 100
US-001001 6 .
PAK-001001 1 70
PAK-001001 2 80
PAK-001001 3 0
PAK-001001 4 0
PAK-001001 5 .
PAK-001001 6 80
;

run;
PeterClemmensen
Tourmaline | Level 20

I'm not sure I understand the requirement. What does your desired result look like?

sahoositaram555
Pyrite | Level 9
Dear @PeterClemmensen,
the resulted data set should look like
AUS-001001 1 520
AUS-001001 2 0
AUS-001001 3 0
AUS-001001 4 0
AUS-001001 5 0
AUS-001001 6 100
US-001001 1 0 100
US-001001 2 100
US-001001 3 100
US-001001 4 100
US-001001 5 100
US-001001 6 100
PAK-001001 1 70
PAK-001001 2 80
PAK-001001 3 0
PAK-001001 4 0
PAK-001001 5 0
PAK-001001 6 80

basically if there is a row blank the value from the upper row must be copied to that place
PeterClemmensen
Tourmaline | Level 20

Ok. Try this

 

data CM;
input USUBJID $10. mmmol_injected;
cards;
AUS-001001  520
AUS-001001  0
AUS-001001  .
AUS-001001  .
AUS-001001  .
AUS-001001  100
US-001001    0
US-001001    .
US-001001    100
US-001001    .
US-001001    100
US-001001    .
PAK-001001  70
PAK-001001  80
PAK-001001  0
PAK-001001  0
PAK-001001  .
PAK-001001  80
;
run;

data want (drop=_:);
   set CM;
   by USUBJID notsorted;
   retain _mm;
   if first.USUBJID then _mm=.;
   if mmmol_injected ne . then _mm=mmmol_injected;
   else if mmmol_injected=. then mmmol_injected=_mm;
run;
sahoositaram555
Pyrite | Level 9
Thank you very much @PeterClemmensen. it worked.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 1406 views
  • 1 like
  • 2 in conversation