Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • foreach and recoding/generate new variables

    Hello all,
    I would like to create the macro for the repetitive work.

    The following is the repetitive work:
    Code:
    recode dr (1=2 "About half the time (36-65%)") (2=4 "Almost always(91-100%)") (3=0 "Almost never (0-10%)") (4=3 "Most of the time (66-90%)") (5=1 "Sometimes(11-35%)"), gen (er_2)
    recode ds (1=2 "About half the time (36-65%)") (2=4 "Almost always(91-100%)") (3=0 "Almost never (0-10%)") (4=3 "Most of the time (66-90%)") (5=1 "Sometimes(11-35%)"), gen (er_3)
    recode dt(1=2 "About half the time (36-65%)") (2=4 "Almost always(91-100%)") (3=0 "Almost never (0-10%)") (4=3 "Most of the time (66-90%)") (5=1 "Sometimes(11-35%)"), gen (er_4)
    I would like to have new variable names as er_#.

    What I came up with is that:
    Code:
    foreach Y of varlist dr-ey {
    forvalues i=2(1)4 {
    recode `Y' (1=2 "About half the time (36-65%)") (2=4 "Almost always(91-100%)") (3=0 "Almost never (0-10%)") (4=3 "Most of the time (66-90%)") (5=1 "Sometimes(11-35%)"), gen (er_`i')
    }
    }
    The commands yield: dr is created to er_2, er_3, and er_4 (Not what I want to).
    Instead, I want to create dr =>er2; ds=>er_3; dt=>er_4.

    I appreciate any comments.
    Best
    Last edited by Yoosun Chu; 24 Sep 2018, 16:50.

  • #2
    That's because you created two loops, one on values i = 2 to 4, nested inside the loop over the variables. What you want is for i to march up in parallel with the iteration over the variables. That would look like this:

    Code:
    local i = 2
    foreach Y of varlist dr-ey {
        recode `Y' (1=2 "About half the time (36-65%)") ///
            (2=4 "Almost always(91-100%)") (3=0 "Almost never (0-10%)") ///
            (4=3 "Most of the time (66-90%)") (5=1 "Sometimes(11-35%)"), gen (er_`i')
        local ++i
    }
    Note: Not tested, as no example data was provided. Beware of typos or other errors.

    Unsolicited advice about coding: while the most important thing about code is that it produce correct results, it is also important that it be readable and understandable to humans. The humans I'm referring to are any other people who might subsequently work with your code, or, for that matter, you when you come back to it a long time from now. One thing that helps with that is indentation. It's a good habit to indent the code within each block that is surrounded by curly braces ({ }). It's also a good habit to break up long lines over several lines, and, optionally, indent the second and subsequent lines of a single command underneath the first line. All of these things make it much easier to see what is going on in the code.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X