Monday, April 09, 2012

Dates in SAS

Suppose you imported data from a spreadsheet with dates that look like 20120409. How can you convert them to the familiar date9 format in SAS?


If they are already in character format, do the following:


datadate2=input(datadate,mmddyy8.);

format datadate2 date9.;

Take care to replace the mmddyy8. depending on how the date is initially written.


If they are in numeric format, then do the following:


date13dchar=put(date13d,8.);

date13d2=input(date13dchar,yymmdd8.);

format date13d2 date9.;

That is, you first convert numeric to character and then character to date. Be sure to keep in mind that this process is vastly made easier if you take care to input the dates in a "nice" format to begin with in the spreadsheet.

Monday, January 16, 2012

How do I identify runs of consecutive observations in panel data?

The STATA mailing list has a way to identify runs of consecutive observations. With some googling, SAS can do the same thing. Here's how.


Suppose you want to figure out how many observations you have per GVKEY:


data merge2; set merge; by gvkey; cnt+1; if first.gvkey then cnt=1; run;

proc sort data=merge2; by gvkey descending cnt; run;

data merge3; set merge2; by gvkey; retain totcnt; if first.gvkey then totcnt=cnt; output; run;