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: variable labels and reshape--reinserting en masse


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: RE: variable labels and reshape--reinserting en masse
Date   Sun, 18 Apr 2010 11:14:58 +0200

<>


So this code probably does what you want (There was also an errant single
right quote in your line " label variable `v' "`myvarlab_'`y'"", I think):


*******
clear
set obs 20
egen byte hh_id = seq(), from(1) to(4) block(5)
bys hh_id: gen byte serial_no=_n
gen biq5  = round(35*uniform())
gen biq6a = round(82*uniform())
gen biq6b = round(4 *uniform())
gen biq8_2= round(16*uniform())
la var biq5 "this is one label"
la var biq6a "this is another label"
la var biq6b "yet another one"
la var biq8_2 "My last and final label"


foreach suffix in 5 6a 6b 8_2{
	local myvarlab_`suffix': variable label biq`suffix'
}

reshape wide biq*, i(hh_id) j(serial_no)
foreach y in 5 6a 6b 8_2 {
  foreach v of varlist biq`y'*{
  label variable `v' "`myvarlab_`y''"
  }
}

aorder
d
*******


HTH
Martin


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Hewan Belay
Sent: Sonntag, 18. April 2010 07:08
To: Stata List
Subject: st: variable labels and reshape--reinserting en masse

Dear Statalist, 

I have been puzzling over this now for a good amount of time, and
unsuccessfully scouring the archives for an example like this. I am trying
to reintroduce variable labels to variables after the dataset has been
reshaped from long to wide. The reason I wasn't able to find the solution in
the archives is: Unlike the cases on the web, I have lots of variables with
the same stub, and want to write a command that can apply for all of them.
To see what I mean, consider the below dataset as a toy example of my case:

clear
set obs 20
g hh_id= 1 in 1/5
replace hh_id= 2 in 6/10
replace hh_id= 3 in 11/15
replace hh_id= 4 in 16/20
by hh_id, s: g serial_no=_n
g biq5  = round(35*uniform())
g biq6a = round(82*uniform())
g biq6b = round(4 *uniform())
g biq8_2= round(16*uniform())
label var biq5 "this is one label"
label var biq6a "this is another label"
label var biq6b "yet another one"
label var biq8_2 "My last and final label"

Now, I want to then reshape this so that each household (hh_id) is one row,
with the variable "serial_no" used to reshape. In other words, after the
reshape, each variable will become 5 variables. In this example I only have
4 "biq" variables, but in my actual dataset I have many, many more,
approximately 45, in other cases more. What I would like to do, is for
example after the reshape, the variables biq51, biq52, ..., biq55 to all
have the same variable label which reads "this is one label". I don't want
to need to use 45 lines to do this. Is there some way I can do something
akin to:

for any 5 6a 6b 8_2: local myvarlab_X : variable label biqX

(in order to save the variable labels. And then next, reshape and retrieve
labels)

reshape wide biq* , i(hh_id) j(serial_no)
foreach y in 5 6a 6b 8_2 {
  foreach v of varlist biq`y' {
  label variable `v' "`myvarlab_'`y'"
  }
}

Unfortunately, the above commands did not work, nor did some variations of
the above that I tried.

Thanks so much for any insights someone may have!
Hewan   


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