Announcement

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

  • Command EXecutes without any error message but makes no changes to the dataset

    Good afternoon all,
    Please this is part of my dataset below
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str32 SignFever str37 SignActivity str48 SignBreathing str49 SignFeeding str32 SignVomiting str48 SignStooling str44 SignSeizures str39 SignsColor float SignsOther
    "Spontaneous" "Spontaneous" "Spontaneous"                      "Prompted"                         "Prompted"                         "Prompted"                         "Prompted"    "Spontaneous" 1
    "Spontaneous" "Spontaneous" "Spontaneous"                      "Spontaneous"                      "Spontaneous"                      "Spontaneous"                      "Spontaneous" "Spontaneous" 0
    "Spontaneous" "Prompted"    "Not familiar with this risk sign" "Not familiar with this risk sign" "Not familiar with this risk sign" "Not familiar with this risk sign" "Prompted"    "Prompted"    1
    "Spontaneous" "Spontaneous" "Prompted"                         "Prompted"                         "Spontaneous"                      "Spontaneous"                      "Spontaneous" "Prompted"    0
    ""            ""            ""                                 ""                                 ""                                 ""                                 ""            ""            .
    end
    label values SignsOther yesno5
    label def yesno5 0 "No", modify
    label def yesno5 1 "Yes", modify

    I have run encode with the following 2 codes


    * Define a macro list of all variables with the same values
    local sign_vars SignFever SignActivity SignBreathing SignFeeding SignVomiting SignStooling SignSeizures SignsOther SignsColor

    foreach var of local sign_vars {
    qui describe `var'

    // Check if variable is string type
    if "`r(typ)'" == "str" {
    qui tab `var' if !missing(`var')
    if r(N) > 0 {
    encode `var', gen(`var'_encoded)
    label define `var'_labels 1 "Spontaneous" ///
    2 "Prompted" ///
    3 "Not familiar with this sign"
    label values `var'_encoded `var'_labels
    drop `var'
    rename `var'_encoded `var'
    }
    }
    }



    // Define a macro list of all variables to be encoded
    local sign_vars SignFever SignActivity SignBreathing SignFeeding SignVomiting SignStooling SignSeizures SignsOther SignsColor

    // Loop through each variable
    foreach var of local sign_vars {
    // Describe variable
    qui describe `var'

    // Check if variable is string type
    if "`r(typ)'" == "str" {
    // Tabulate non-missing values
    qui tab `var' if !missing(`var')

    // Check if there are non-missing values
    if r(N) > 0 {
    // Encode the variable
    encode `var', gen(`var'_encoded)

    // Define labels
    label define `var'_labels 1 "Spontaneous" ///
    2 "Prompted" ///
    3 "Not familiar with this sign"

    // Apply labels
    label values `var'_encoded `var'_labels

    // Drop original variable
    drop `var'

    // Rename encoded variable to original variable name
    rename `var'_encoded `var'
    }
    }
    }



    At the end of each, and severally, it executes without any error messages but makes no changes to the dataset. They remain as string variables. What may be wrong please? I am using Stata 18

    Obumneme Ezeanosike

    Last edited by OBUM EZEANOSIKE; 09 Feb 2024, 08:49.

  • #2
    I am using Stata 18

    Comment


    • #3
      If the definition of the local macro is not visible to the foreach statement, then you are asking for a loop over an empty set. That's perfectly legal but necessarily nothing will happen.

      A common way in which this bites is if you're executing code line by line from the do-file editor.

      A separate puzzle is that I wouldn't expect to find r(typ) defined after describe. That comparison is not illegal but you will be comparing "." for equality with "str" which will never be true.

      Comment


      • #4
        Thank you, Nick. Please can you suggest the correction or alternative to the code?
        Obumneme

        Comment


        • #5
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str32 SignFever str37 SignActivity str48 SignBreathing str49 SignFeeding str32 SignVomiting str48 SignStooling str44 SignSeizures str39 SignsColor float SignsOther
          "Spontaneous" "Spontaneous" "Spontaneous"                      "Prompted"                         "Prompted"                         "Prompted"                         "Prompted"    "Spontaneous" 1
          "Spontaneous" "Spontaneous" "Spontaneous"                      "Spontaneous"                      "Spontaneous"                      "Spontaneous"                      "Spontaneous" "Spontaneous" 0
          "Spontaneous" "Prompted"    "Not familiar with this risk sign" "Not familiar with this risk sign" "Not familiar with this risk sign" "Not familiar with this risk sign" "Prompted"    "Prompted"    1
          "Spontaneous" "Spontaneous" "Prompted"                         "Prompted"                         "Spontaneous"                      "Spontaneous"                      "Spontaneous" "Prompted"    0
          ""            ""            ""                                 ""                                 ""                                 ""                                 ""            ""            .
          end
          label values SignsOther yesno5
          label def yesno5 0 "No", modify
          label def yesno5 1 "Yes", modify
          
          qui ds
          local order "`r(varlist)'"
          label define sign 1 "Spontaneous" 2 "Prompted" 3 "Not familiar with this risk sign"
          foreach var of varlist SignFever - SignsColor{
              encode `var', gen(`=lower("`var'")') label(sign)
              drop `var'
              rename `=lower("`var'")' `var'
          }
          order `order'
          Res.:

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input long(SignFever SignActivity SignBreathing SignFeeding SignVomiting SignStooling SignSeizures SignsColor) float SignsOther
          1 1 1 2 2 2 2 1 1
          1 1 1 1 1 1 1 1 0
          1 2 3 3 3 3 2 2 1
          1 1 2 2 1 1 1 2 0
          . . . . . . . . .
          end
          label values SignFever sign
          label values SignActivity sign
          label values SignBreathing sign
          label values SignFeeding sign
          label values SignVomiting sign
          label values SignStooling sign
          label values SignSeizures sign
          label values SignsColor sign
          label def sign 1 "Spontaneous", modify
          label def sign 2 "Prompted", modify
          label def sign 3 "Not familiar with this risk sign", modify
          label values SignsOther yesno5
          label def yesno5 0 "No", modify
          label def yesno5 1 "Yes", modify

          Comment


          • #6
            Thank you Andrew, both examples worked well.

            Comment

            Working...
            X