Strings to unique integers in Stata

Posted in code on March 22nd, 2009 by Michael Ewens – Be the first to comment

Suppose you have a string variable with a finite number of values (e.g. US states, industry, etc). The encode command plus the generate command will create a new variable that assigns unique ids to each string value:

encode your_string_variable, gen(new_string_as_int)

The labels in the edit/browse view will look like strings, so make sure you use ‘nolabel’ on any export or tab commands.

Clean Dates With CRSP Data in Stata

Posted in code on March 21st, 2009 by Michael Ewens – Be the first to comment

Here is some simple code to transform those pesky CRSP dates (YYYYMMDD) to Stata compatible dates:
insheet using "sp_daily.csv"
rename caldt date
* Date Transformation gen year = floor(date/10000)
gen month = floor((date - year*10000)/100)
gen day = floor(date - year*10000 - 100*month)
* replace the date
replace date = mdy(month, day, year)
format date %td