Announcement

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

  • replacing variable responses using loop

    I am new to learning the loop command and could not find help from google. I am trying to use the foreach loop command to change all LEGITIMATE SKIP and RESPONDENT SKIP to missing. The label numbers for them are different in different variables that’s why I am struggling with how I can use the value label “LEGITIMATE SKIP” and “RESPONDENT SKIP” (which are responses in all variables) in the loop command.
    I tried:
    foreach v of varlist gender age ethnicity income employment {
    replace `v’ = . if `v’ == “LEGITIMATE SKIP”
    }

    This is giving me an error. Is there a way to do this?
    Thanks!

  • #2
    Welcome to Statalist. Please present your data using the dataex command.

    Comment


    • #3
      Jared Greathouse is correct: a solution to your problem cannot be offered without having example of your data, using the -dataex- command. However, the nature of the problem can be fully explained.

      The problem has nothing at all to do with looping. The problem is with the command -replace `v’ = . if `v’ == “LEGITIMATE SKIP”-. It will always give you a type mismatch because it is self-contradictory. If `v' is a numeric variable, then "LEGITIMATE SKIP", a string, is not a possible value. If `v' is a string variable, then . is not a possible value. I suspect that what you have is numeric variables that are value-labeled. There is a way to handle those. But without seeing the details, it cannot be specified.

      Comment


      • #4
        You are right, it is giving me a type mismatch. Not sure how to solve this. These are all categorical variables, where LEGITIMATE SKIP and RESPONDENT SKIP are value labels in blue that is actually 1, 2, 3. Here are the details:

        . dataex NOINS_QUAL NOINS_OFFRDEMP NOINS_AFFRDEMP NOINS_ACCESS

        ----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(NOINS_QUAL NOINS_OFFRDEMP NOINS_AFFRDEMP NOINS_ACCESS)
        2 2 2 2
        4 4 4 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        3 4 4 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        1 1 1 1
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        3 3 3 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        4 4 4 4
        2 2 2 2
        4 4 3 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        4 4 4 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        3 3 4 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        4 4 4 4
        2 2 2 2
        2 2 2 2
        3 4 4 4
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        2 2 2 2
        end
        label values NOINS_QUAL labels5
        label def labels5 1 "RESPONDENT SKIP", modify
        label def labels5 2 "LEGITIMATE SKIP", modify
        label def labels5 3 "Yes", modify
        label def labels5 4 "No", modify
        label values NOINS_OFFRDEMP labels7
        label def labels7 1 "RESPONDENT SKIP", modify
        label def labels7 2 "LEGITIMATE SKIP", modify
        label def labels7 3 "Yes", modify
        label def labels7 4 "No", modify
        label values NOINS_AFFRDEMP labels8
        label def labels8 1 "RESPONDENT SKIP", modify
        label def labels8 2 "LEGITIMATE SKIP", modify
        label def labels8 3 "Yes", modify
        label def labels8 4 "No", modify
        label values NOINS_ACCESS labels9
        label def labels9 1 "RESPONDENT SKIP", modify
        label def labels9 2 "LEGITIMATE SKIP", modify
        label def labels9 4 "No", modify
        ------------------ copy up to and including the previous line ------------------

        Listed 100 out of 2794 observations
        Use the count() option to list more

        .

        Comment


        • #5
          Now with a data example, the reason is even more clear. Instead of
          Code:
          foreach v of varlist gender age ethnicity income employment {
          replace `v’ = . if `v’ == “LEGITIMATE SKIP”
          }
          What you want (i think, I'm not at my computer to test this) is
          Code:
          foreach v of varlist gender age ethnicity income employment {
          replace `v’ = . if `v’ == 2
          }

          Comment


          • #6
            Thanks Jared, that's what I have been doing, but I am working with a lot more variables (over 50), and LEGITIMATE SKIP or RESPONDENT SKIP don't have the same value labels (like in one variable it may be ==2, in another, it may be ==7), therefore I am trying to identify them with the value labels.

            Comment


            • #7
              Show me the results of
              Code:
              foreach v of varlist gender age ethnicity income employment {
              
              tab `v’ 
               }

              Comment


              • #8
                Code:
                foreach v of varlist NOINS_QUAL NOINS_OFFRDEMP NOINS_AFFRDEMP NOINS_ACCESS {
                    local lbl: val label `v'
                    local tgt = "LEGITIMATE SKIP":`lbl'
                    replace `v' = . if `v' == `tgt'
                }
                will work even when "LEGITIMATE SKIP" corresponds to different numeric values in the different variables.

                Comment


                • #9
                  You're right. I still need to get the hang of extracting labels like this, but yes, this is better than my original suggestion Clyde Schechter

                  Comment


                  • #10
                    Thanks, Clyde Schechter and Jared Greathouse , this really worked and saved me a lot of time.

                    Comment

                    Working...
                    X