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

I have this table

 

ID   Period   Value

A    1            10

A    2            50

A    3             30

B    1             20

B    2             50

 

I want to make a table that extends for each ID to period 5, filling the previously non-existent values with the last existing value. Like this.

 

ID   Period   Value

A    1            10

A    2            50

A    3             30

A    4             30

A    5             30

B    1             20

B    2             50

B    3             50

B    4             50

B    5             50

 

In the existing table, the last period could be different. How do I program this? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It should be straightforward as long as your data set is sorted:

 

data want;

set have;

by id period;

output;

if last.id and period < 5 then do period = period + 1 to 5;

   output;

end;

run;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

It should be straightforward as long as your data set is sorted:

 

data want;

set have;

by id period;

output;

if last.id and period < 5 then do period = period + 1 to 5;

   output;

end;

run;

apolitical
Obsidian | Level 7

Astoundingly simple solution. Thanks!

ChrisNZ
Tourmaline | Level 20

This looks like homework, What have you tried?

apolitical
Obsidian | Level 7

Unfortunately this is not homework and there is no buddy to copy from...

ChrisNZ
Tourmaline | Level 20

@Astounding's code is the answer.

 

You can even shorten it slightly.

 

if last.id then do period = period + 1 to 5;

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