Announcement

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

  • Code not working for a particular variable when it worked for others

    I am trying to extract the numbers that begins the value of variables. The code works for apartipcprogram_component6 and ademodemographics2 but not aoteother_prog_exp1. I do not know what the problem is with aoteother_prog_exp1 that the code is not working for it. I will appreciate your input. Thanks.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str23 apartipcprogram_component6 str5(aoteother_prog_exp1 ademodemographics2)
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  "1_yes" "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    "1_client_answered" ""      "1_yes"
    ""                  ""      ""     
    ""                  "2_no"  "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "1_yes" "1_yes"
    ""                  "2_no"  "1_yes"
    ""                  "1_yes" "1_yes"
    end
    Code:
    global vars2 "apartipcprogram_component6 aoteother_prog_exp1 ademodemographics2"
    foreach v of global vars2 {
        capture confirm string variable `v'
        if !missing(`v') & !_rc {
            replace `v' = substr(`v',1,strpos(`v',"_")-1)
            destring `v', replace
            } 
    }

  • #2
    Your code has confused -if- qualifier and -if- command. What you need is:
    Code:
    local vars2 "apartipcprogram_component6 aoteother_prog_exp1 ademodemographics2"
    foreach v of local vars2 {
        capture confirm string variable `v'
        if !_rc {
            replace `v' = substr(`v',1,strpos(`v',"_")-1) if !missing(`v')
            destring `v', replace
            }
    }
    The first -if- in the code is an if command. It determines whether the entire block of code in the braces will be executed or not. It does not handle variables in the way you might expect. When you wrote -if !missing(`v') & !_rc -, it does not inspect the individual observations and apply them only to those where `v' is non-missing. Rather it looks only at the value of `v' in the first observation. For the variable aoteother_prog, that first value is missing. So the condition was not met, and the entire block of code between the braces was skipped. What you intended is, to apply the code in the braces only to those observations where `v' is non-missing. But the -if- command doesn't, and cannot, do that. That is the job of the -if- qualifier, which follows, not precedes, the one specific command it applies to.

    By the way, I have changed your global macro to local. This does not affect the error you were getting in the original code. I have done it only because the use of global macros for the purpose of storing a list of variables like this is an unsafe programming practice that should be avoided. Global macros are inherently unsafe and should be used only when there is no alternative. In over 29 years of near-daily use of Stata I have only encountered one situation where a global macro was truly needed. Whenever you are tempted to write a global in Stata, think again and ask yourself whether it is really needed. A local macro will almost always serve the same purpose. And if it doesn't, there are usually other ways of doing it that avoid using globals.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Your code has confused -if- qualifier and -if- command. What you need is:
      Code:
      local vars2 "apartipcprogram_component6 aoteother_prog_exp1 ademodemographics2"
      foreach v of local vars2 {
      capture confirm string variable `v'
      if !_rc {
      replace `v' = substr(`v',1,strpos(`v',"_")-1) if !missing(`v')
      destring `v', replace
      }
      }
      The first -if- in the code is an if command. It determines whether the entire block of code in the braces will be executed or not. It does not handle variables in the way you might expect. When you wrote -if !missing(`v') & !_rc -, it does not inspect the individual observations and apply them only to those where `v' is non-missing. Rather it looks only at the value of `v' in the first observation. For the variable aoteother_prog, that first value is missing. So the condition was not met, and the entire block of code between the braces was skipped. What you intended is, to apply the code in the braces only to those observations where `v' is non-missing. But the -if- command doesn't, and cannot, do that. That is the job of the -if- qualifier, which follows, not precedes, the one specific command it applies to.

      By the way, I have changed your global macro to local. This does not affect the error you were getting in the original code. I have done it only because the use of global macros for the purpose of storing a list of variables like this is an unsafe programming practice that should be avoided. Global macros are inherently unsafe and should be used only when there is no alternative. In over 29 years of near-daily use of Stata I have only encountered one situation where a global macro was truly needed. Whenever you are tempted to write a global in Stata, think again and ask yourself whether it is really needed. A local macro will almost always serve the same purpose. And if it doesn't, there are usually other ways of doing it that avoid using globals.
      Thank you very much. It works now. I will strongly heed your advice on the use of local.

      Comment

      Working...
      X