Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: RE: extract labels from first row of data


From   "Cohen, Elan" <[email protected]>
To   "'[email protected]'" <[email protected]>
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: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Will Hauser
> Sent: Tuesday, September 20, 2011 11:46
> To: [email protected]
> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index