Economics should be open

October 2, 2008

stata transpose string variable without xpose

Filed under: coding, Data Insights, Stata — Tags: , , , , — howardchong @ 10:52 pm

So STATA will let you transpose the data with the xpose command, but this does not handle string data.

 

PROBLEM:

I had a set of stock price series. Variable names were data and the stock codes. rows were days

 

DATE  - STOCK1 - STOCK2 ... -STOCKN

1/1/2005   $1    $5   $10

...

12/31/2005 ...

 

So, I managed to do it as follows:

1) First, rename all stock variables “price”+STOCKNAME

foreach vn of varlist STOCK1-STOCKN {
  quiet: rename `vn’ price`vn’
}

2) reshape long
3) reshape wide

 

reshape long price, i(realdate) j(name) string
drop date
reshape wide price, i(name) j(realdate)

 

Note that I needed realdate to be an integer, so I ran a
gen realdate=date(datestr,”mdy”)
and then dropped date.
If I keep date as a string, I can’t have the slashes in the string variable name, so you do have to somehow convert it to something you want. You can replace the slashes with underscores and then add the “string” argument to the second reshape.

Advertisements

1 Comment »

  1. […] stata transpose string variable without xpose « Economics should be open. […]

    Pingback by Transposing and reshaping with string in Stata | Michael Kevane — December 17, 2012 @ 9:35 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: