Bring value labels to variable labels when reshapi

2019-08-11 08:17发布

问题:

In Stata, I would like to be able to bring value labels up into variable labels when reshaping wide.

My raw data looks like this:

patient hosp_id     hosp_name               charges
Andrew  1           Springfield General     $10 
Barry   1           Springfield General     $20 
Crista  2           Lincoln Medical Center  $10 
Doris   2           Lincoln Medical Center  $15 
Ellen   1           Springfield General     $15 
Faye    3           Memorial Hospital       $35 

I then label the values of hosp_id and reshape wide.

label define hosp_names 1 "Springfield General" 2 "Lincoln Medical Center" 3 "Memorial Hospital"
label value hosp_id hosp_names
reshape wide charges, i(patient) j(hosp_id)

I would like the labels that I placed on hosp_id to follow their associated values and become variable labels. I know that I can label the variables one by one after reshaping, but this may be impractical with a much larger set of j values (in this case hospitals). How can I label each of the hosp_id variables with the associated value of hosp_name programmatically?

回答1:

In general, applying reshape and also carrying across variable or value labels can be awkward, but see documentation at http://www.stata.com/support/faqs/data-management/apply-labels-after-reshape/ for detailed discussion.

In your case, what your reshape code does is to work on charges and make a separate variable for each distinct hospital. (Your question states: How can I label each of the hosp_id variables with the associated value of hosp_name programmatically? but hosp_id is carried over unchanged.)

There is a much more direct way to do that using separate. Here is an example almost identical to yours.

. input str6 patient hosp_id  str22 hosp_name  charges

    patient    hosp_id               hosp_name    charges
 1. "Andrew"  1           "Springfield General"     10 
 2. "Barry"   1           "Springfield General"     20 
 3. "Crista"  2           "Lincoln Medical Center"  10 
 4. "Doris"   2           "Lincoln Medical Center"  15 
 5. "Ellen"   1           "Springfield General"     15 
 6. "Faye"    3           "Memorial Hospital"       35 
 7. end 

. separate charges, by(hosp_name) veryshortlabel 

               storage  display     value
variable name   type   format      label      variable label
----------------------------------------------------------------------------------------
charges1        byte   %9.0g                  Lincoln Medical Center
charges2        byte   %9.0g                  Memorial Hospital
charges3        byte   %9.0g                  Springfield General

The option veryshortlabel is undocumented in help and manuals, but useful here. It is not compulsory, naturally. There was a mention in http://www.stata-journal.com/sjpdf.html?articlenum=gr0023 In essence, veryshortlabel was an after-thought after separate was first written.



回答2:

Short, simple and gets the job done: install varlab module, more info from https://ideas.repec.org/c/boc/bocode/s425001.html

ssc install varlab

1 .Then just save your variable labels varlab save [varlist] using filename [, replace ]

  1. Once your dataset reshaped, load them varlab load [varlist] using filename [, replace ]

Hint: if as usually the variable names change after reshaping( "var_year01" ,"var year_02" in wide form TO "var_" in long form ) , just open the label file(filename) created in 1. and modify the names using the Do editor.

Good Luck



标签: stata