Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: data manipulation question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: data manipulation question
Date   Tue, 26 Jun 2007 22:02:01 +0100

Starting from where you are now, 

foreach v of var iso_* { 
	local newname "`: var label `v''"
	local newname : subinstr local newname "iso==" ""  
	rename `v' `newname'
} 

Starting from your original -iso_str-: 

levelsof iso_str, local(levels) 
foreach l of local levels { 
	gen byte `l' = iso_str == "`l'" 
}

However, this example really hinges on the fact that 
your string values happen all to be legal variable names. 

Nick 
[email protected] 

David Airey
 
> I have a variable naming question, after encoding.
> 
> I used these commands,
> 
> encode iso_str, generate(iso)
> tab iso, gen(iso_)
> 
> "iso_str" was a variable with these levels:
> 
> . levels iso_str
> `"IDI"' `"IDV"' `"IGI"' `"IGV"' `"INI"' `"INV"' `"ISI"' `"ISV"'  
> `"MDV"' `"MNI"'
>  > `"MNV"' `"MSI"' `"MSV"' `"VDI"' `"VDV"' `"VGI"' `"VGV"' `"VNI"'  
> `"VNV"' `"VSI"
>  > ' `"VSV"'
> 
> and so iso has these levels
> 
> . levels iso
> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
> 
> after tab iso, gen(iso_)
> 
> I wind up with 1,0 indicator variables for each level of iso, with  
> useful variable labels. But really I'd like to have indicator  
> variables named for each level, like "IDI" instead of 
> "iso_1". How do  
> I do that?
> 
> 
>                storage  display     value
> variable name   type   format      label      variable label
> --------------------------------------------------------------
> ---------- 
> -------
> iso_str         str3   %9s
> iso             long   %8.0g       iso
> iso_1           byte   %8.0g                  iso==IDI
> iso_2           byte   %8.0g                  iso==IDV
> iso_3           byte   %8.0g                  iso==IGI
> iso_4           byte   %8.0g                  iso==IGV
> iso_5           byte   %8.0g                  iso==INI
> iso_6           byte   %8.0g                  iso==INV
> iso_7           byte   %8.0g                  iso==ISI
> iso_8           byte   %8.0g                  iso==ISV
> iso_9           byte   %8.0g                  iso==MDV
> iso_10          byte   %8.0g                  iso==MNI
> iso_11          byte   %8.0g                  iso==MNV
> iso_12          byte   %8.0g                  iso==MSI
> iso_13          byte   %8.0g                  iso==MSV
> iso_14          byte   %8.0g                  iso==VDI
> iso_15          byte   %8.0g                  iso==VDV
> iso_16          byte   %8.0g                  iso==VGI
> iso_17          byte   %8.0g                  iso==VGV
> iso_18          byte   %8.0g                  iso==VNI
> iso_19          byte   %8.0g                  iso==VNV
> iso_20          byte   %8.0g                  iso==VSI
> iso_21          byte   %8.0g                  iso==VSV

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index