Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Incorporating variable labels into the variable name


From   "Michael Blasnik" <michael.blasnik@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Incorporating variable labels into the variable name
Date   Fri, 07 Sep 2007 17:32:19 -0400

...

I think you will have to tell insheet that you don't have variable names and then rename each variable based on the first observation, then drop that observation, and then destring everything numeric. Here's a rough cut (untested):

insheet using myfile.csv, nonames
foreach var of varlist _all {
local vname=`var'[1]
local date=date("`vname'","mdy")
if "`date'"!="." {
local year=year(`date')
local monthday:di %dmd `date'
local vname= "rev_"+trim("`monthday'")+"_"+trim("`year'")
}
rename `var' `vname'
}

drop in 1
destring rev_*, replace

M Blasnik

----- Original Message ----- From: "Rodriguez, Dan" <dan.rodriguez@credit-suisse.com>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, September 07, 2007 4:41 PM
Subject: st: Incorporating variable labels into the variable name



I have a comma-delimited datafile that I need to process on a daily basis within Stata where I need to convert the labels for about 200 columns into variable names. The file looks like the following:

acct name 9/5/2007 9/4/2007 8/31/2007
84J Jacky 1 2 -1
B88 Mike -2 3 -1
B93 Mike 4 5 2


When I import this file into Stata I get:

acct name v18 v19 v20
84J Jacky 1 2 -1
B88 Mike 0 0 0
B93 Mike 0 0 0

. desc
<snip>
-------------------------------------------------------------------------------
acct     str8   %9s                    Trading Acct
name         str53  %53s                   Trader
v18             float  %9.0g                  9/5/2007
v19             float  %9.0g                  9/4/2007
<snip>

How can I change the variable names to rev_Sep5_2007, rev_Sep4_2007, rev_Aug31_2007, etc.?

I need to apply a reshape command to facilitate further analysis.

Thank you,
Dan Rodriguez
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index