Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: RE: RE: destringing and replacing values for several variables |

Date |
Thu, 22 Apr 2010 15:25:37 +0100 |

-destring- is a marvellous command, but I agree with Martin that -encode- is the way to go here. By the way, -multencode- from SSC lets you multiply encode based on the same value labels. Nick n.j.cox@durham.ac.uk Martin Weiss Glad it worked out for you! Re loops, you may want to try NJC`s http://www.stata-journal.com/sjpdf.html?articlenum=pr0005 Ann Miller Thank you very much for this reply. This one in particular worked beautifully and -encode- saved me several steps. "Martin Weiss" <martin.weiss1@gmx.de> 4/21/2010 3:51 PM >>> Ann, you could -encode- one of those "hf_stage*" fellows outside of your loops, and then apply the same -label- to everybody else inside a loop: encode hf_stage1, gen(hf_stage1numeric) label(myhflabel) foreach var of varlist hf_stage2-hf_stage40{ encode `var', gen(`var'1numeric) label(myhflabel) } la list myhflabel ta hf_stage1numeric ta hf_stage1numeric, nolabel If anybody wants to put himself/herself in Ann`s shoes, this is what her data look like in my view: ******* clear* set obs 100 foreach var of newlist hf_stage1-hf_stage40{ gen str10 `var'="Stage 1" replace `var'="Stage 2" in 1/30 replace `var'="Stage 3" in 39/40 replace `var'="Stage 4" in 46/80 } ******* Ann Miller I am new to macros and the foreach looping and am trying to simplify my life. I have a list of 40 or so variables (ex: hf_stage1 through hf_stage40) that all have the same string coded values; "Stage 1" "Stage 2" "Stage 3" "Stage 4". I want to change these values to 1, 2, 3 and 4, with value labels of "Stage 1" "Stage 2" "Stage 3" and "Stage 4". What I have done in the past is: replace hf_stage1="1" if hf_stage1=="Stage 1" replace hf_stage1="2" if hf_stage1=="Stage 2", etc., destring hf_stage1,replace and for each value generate a label. I then have to repeat this for every variable (hf_stage2 through hf_stage40). It works, but is time consuming and I sometimes make mistakes. I feel sure there's a way to do this with macros and foreach but there's something I'm missing so far. Here's what I've tried: local varlistfoo "hf_stage1 hf_stage2 hf_stage3" . foreach var of `varlistfoo' { 2. replace var= "1" if var=="Stage 1" 3. replace var= "2" if var=="Stage 2" 4. replace var= "3" if var=="Stage 3" 5. } invalid syntax r(198); . foreach varname of varlist hf_stage1 hf_stage2 hf_stage3{ 2. replace varname= "1" if varname=="Stage 1" 3. replace varname= "2" if varname=="Stage 2" 4. } variable varname not found r(111); I can't even get the first step, let alone destringing and replacing! Any advice you all can offer would be greatly appreciated.Thank you! * * 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/

**References**:**st: destringing and replacing values for several variables***From:*"Ann Miller" <MILLERA@hsph.harvard.edu>

**st: RE: destringing and replacing values for several variables***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**st: RE: destringing and replacing values for several variables***From:*"Ann Miller" <MILLERA@hsph.harvard.edu>

**st: RE: RE: destringing and replacing values for several variables***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**st: IVTobit with three endogenous variables** - Next by Date:
**Re: st: Heteroskedastic Probit Model** - Previous by thread:
**st: RE: RE: destringing and replacing values for several variables** - Next by thread:
**st: Graph export: "Could not initialize metafile"** - Index(es):