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 on April 23, and its replacement, statalist.org is already up and running.


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

st: RE: extract labels from first row of data


From   "Cohen, Elan" <cohened@upmc.edu>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: extract labels from first row of data
Date   Tue, 20 Sep 2011 17:16:21 +0000

foreach var of varlist * {
  label variable `var' "`=`var'[1]'"
  replace `var'="" if _n==1
  destring `var', replace
}

HTH,

- Elan


> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Will Hauser
> Sent: Tuesday, September 20, 2011 11:46
> To: statalist@hsphsun2.harvard.edu
> Subject: st: extract labels from first row of data
> 
> I'm attempting to import census data using the insheet command and the data
> is organized such that the variable names are on the first row and the
> labels are on the second.  Using the 'names' option gets me a dataset with
> correct variable names and the labels are in the first observation.
> 
> After searching the archives I believe I have to extract the labels using a
> foreach loop and then destring the variables but I can't seem to structure
> the loop correctly.  I'm also wondering if there's a user written program to
> do this.  It seems a little bizarre to me that a program as amazingly
> versatile as Stata would require a workaround to import something as
> universally used as US Census data.
> 
> Here's what I've tried so far using Stata 12:
> insheet using "C:\Users\Will\CensusData.txt", delimiter("|") names clear
> case
> 
> (GEO is the first variable in the dataset and PO13002 is the last)
> 
> foreach var of varlist (GEO_ID-P013002) {
> 	label variable `var' "`var'" if (_n==1)
> 	}
> 
> I know I need to reference the content of the variable in obs 1 and I'm not
> sure how to do that.
> 
> Thanks,
> 
> Will Hauser
> 
> *
> *   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/


*
*   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