Announcement

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

  • Using loops to generate and recode variables

    I want to recode 4 variables the same way using loops. I followed the instructions of Scott Long on the Workflow of Data Analysis (pg 99)

    Here is my code:

    local likehert "prepquestn askquestn discquestn confiquestn" /*likehert is the name of the macro for my 4 variables*/

    foreach varname of varlist 'likehert' {
    generate t'varname' = 'varname' /* I want a new variable name that adds a 't' to the source variable (e.g tprepquestn from prepquestn )*/
    replace t'varname' = 1 if 'varname' == 2 /*I want to replace values from the old variable into the new variable.*/
    replace t'varname' = 2 if 'varname' == 4
    replace t'varname' = 3 if 'varname' == 5 |'varname' == 6
    replace t'varname' = 4 if 'varname' == 3
    replace t'varname' = 5 if 'varname' == 1
    }

    The error message from STATA is ' invalid name.

    Myself and a colleague have examined this closely, but I do not know where we are going wrong.

    Thanks

  • #2
    You are using the apostrophe(') character to open your macro references, when you should be using the left-quote (`) character. The left-quote character, on a US keyboard, is located just to the left of the key for number 1. (The apostrophe is, however, used to close the macro reference.) So it should be:

    Code:
    gen t`varname' = `varname'
    // etc.
    Note, by the way that you can do this more simply with the -recode- command:

    Code:
    foreach v of varlist `likehert' {
        gen t`v' = `v'
        recode `v', (2=1) (4=2) (5/6 = 3) (3=4) (1=5)
    }

    Comment


    • #3
      Clydes code should read

      Code:
      recode `v', (2=1) (4=2) (5/6 = 3) (3=4) (1=5)
      note absence of the comma

      Here is a way without looping

      Code:
      local likehert ...
      rename (`likehert')(t=)
      recode `r(newnames)' (2 = 1) ... , generate(`r(oldnames)')
      Best
      Daniel

      Comment


      • #4
        Daniel is right that there should be no comma in the -recode- command. My error. I note, however, that he made the same error himself!

        Comment


        • #5
          Indeed. Sorry about that. Just to get it right

          Code:
          recode `v' (2=1) (4=2) (5/6 = 3) (3=4) (1=5)
          Best
          Daniel

          Comment


          • #6
            Clyde Schechter and daniel klein,

            Thank you very much. You have made my day and saved me from frustration.

            I have one more question. How do you declare an array in STATA? Is there some ado file I have to download? I ask because my first line of command did not work:

            local likehert "prepquestn askquestn discquestn confiquestn" /*likehert is the name of the macro for my 4 variables*/

            Eventually, I went with this:

            foreach v of varlist prepquestn askquestn discquestn confiquestn {
            generate t`v' = . //etc

            Thanks again for your support.
            Marvellous

            Comment


            • #7
              Your command
              [code]
              local likehert "prepquestn askquestn discquestn confiquestn" /*likehert is the name of the macro for my 4 variables*/
              [/quote]
              works just fine for me. Here's a thought: are you running this code by typing it into the command window? Comments are not allowed in the command window and are considered syntax errors in that context. The code segments we have shown you are intended to be run as do-files. In fact, any serious work in Stata should always be coded in a do file and executed from that do-file so that 1) you can reproduce the results without additional work later if need be, and 2) you have documentation of what you did. Production work should never be done by typing from the command window.

              As for declaring an array in Stata, Stata has a matrix type. See -help matrix-. Note that Stata matrices can contain only numbers, not strings. If you need an array of strings you will have to learn and use Mata.

              Comment


              • #8
                Clyde, thanks a lot for your insight.

                Comment

                Working...
                X