Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Importing data with improper variable names


From   Austin Nichols <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Importing data with improper variable names
Date   Tue, 3 Aug 2010 12:28:48 -0400

Glenn Hoetker <ghoetker@igb.illinois.edu>:

You could write a little do file that does all of the requisite
preprocessing.  Try these suggestions out:

file open fh using /temp.txt, write
file write fh `" "Name", "2001", "2002", "2003""' _n
file write fh  `" "Fred", "3", "3", "0" "' _n
file close fh

insheet using /temp.txt, clear
l, noo
forv i=1/4 {
if real("`=v`i'[1]'")==. {
 ren v`i' `=v`i'[1]'
 }
else {
 ren v`i' `="y"+string(`=v`i'[1]')'
 }
}
drop in 1
l, noo
qui destring, replace
reshape long y, i(Name) j(Year)
l, noo
erase /temp.txt


On Tue, Aug 3, 2010 at 12:07 PM, Glenn Hoetker
<ghoetker@igb.illinois.edu> wrote:
> Greetings all.
>
> I have data downloaded as a text file from a website that comes in a form like
>
>        "Name", "2001", "2002", "2003"
>        "Fred", "3", "3", "0"
>
> It's just a text file, so I'd normally just use
>
>        insheet using data.text, names
>
> However, since variable names can't begin with a number, the last variables get renamed v3 v4 v5.  That's inconvenient, because it's meant to be a year.   I can manually rename v3 to year2001, etc., but would rather not do so--I'll be importing this data fairly often,  v3 may not always map to 2001, and there are actually many more years than I've shown here.
>
> Can anyone suggest a way around this problem?  I'm using Stata 11 and have access to StatTransfer.  I'm on a Mac, giving me access to both AppleScript and the standard Unix suite of tools to pre-process the file, but I'm rusty with those and would prefer a "pure Stata" solution if feasible.
>
> Thank you for any pointers.
>
> Glenn

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index