BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jarvin99
Obsidian | Level 7
data have;  
   input gvkey $ year shrcd;  
   datalines;  
2477 2001 11
2477 2002 11
2477 2003 
3156 2001 10
3156 2002 10
3156 2003 
3156 2004 
3010 2001 
3010 2002 
3010 2003 
3010 2004 
;  
proc print data=have;  
run;

Hi everyone,

I am new to SAS, so the data step to input the data that I am writing here may contain some flaws, but I did my best to help you visualize my panel.  I only want to fill in the column "shrcd" (as there actually many columns in my dataset with missing values) if this gvkey once had its shrcd filled in. 

Hence, gvkey 2477 should have its 2003's shrcd as 11, and gvkey 3156 should have both its 2003 and 2004 filled in as 10, but gvkey 3010 should NOT have anything filled in for its shrcd.

 

Could I kindly seek assistance on how to do my request? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can use the UPDATE statement to implement last observation carried forward.  To prevent it from applying to the other variables just re-read the observation without the ones you do want carried forward.

data want;
  update have(obs=0) have;
  by gvkey;
  set have(drop=shrcd);
  output;
run;

If you want to move values backwards in time then you will need a different method.

View solution in original post

2 REPLIES 2
sbxkoenk
SAS Super FREQ

If you are working with Time Series Data and Panel data, you will have SAS/ETS and SAS Econometrics available.
So you can use PROC EXPAND for this task.

data have;  
   input gvkey $ year shrcd;
   yeardt=MDY(12,31,year);
   format yeardt date9.; 
   datalines;  
2477 2001 11
2477 2002 11
2477 2003 . 
3156 2001 10
3156 2002 10
3156 2003 .
3156 2004 .
3010 2001 .
3010 2002 .
3010 2003 .
3010 2004 .
;  
*proc print data=have; *run;
*proc sort data=have; *by gvkey yeardt; *run;

proc expand data=have out=want 
            method=step EXTRAPOLATE;
 by gvkey notsorted;
 id yeardt;
 var shrcd;
run;
proc print data=want; run;
/* end of program */

Koen

Tom
Super User Tom
Super User

You can use the UPDATE statement to implement last observation carried forward.  To prevent it from applying to the other variables just re-read the observation without the ones you do want carried forward.

data want;
  update have(obs=0) have;
  by gvkey;
  set have(drop=shrcd);
  output;
run;

If you want to move values backwards in time then you will need a different method.

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