Announcement

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

  • How to standardize Stata variables format before using -append- ?

    I have around 40 files that I would like to append together. However, two variables named a12 and a13 are problematic because they can take three different shapes :

    - Numeric
    - String with only numeric values
    - String with alphanumeric values

    I guess the fastest way to convert them would be to turn the numeric a12 and a13 variables into string variables, but when running the code:

    Code:
    foreach co of global countries{
        use ``country'_finaldata', clear 
        foreach appendvar of varlist a12 a13 {
            tostring `appendvar', replace
        }
        save ``country'_finaldata', replace
    }
    I get the following result :

    Code:
    a12 has value label; no replace
    a13 has value label; no replace
    Can't I use the replace option for these variables ? If not, what would be an efficient programming to turn all the variables into string ones?

  • #2
    Something like this? (warning: not tested, in the absence of a data example; please use dataex in future)

    Code:
    foreach country of global countries {
        use ``country'_finaldata', clear
        foreach appendvar of varlist a12 a13 {
            local val_lab: value label `appendvar'
            if "`val_lab'" != "" {
                decode `appendvar', gen(_`appendvar')
                drop `appendvar'
                rename _`appendvar' `appendvar'
            }
                else tostring `appendvar', replace
        }
        save ``country'_finaldata', replace
    }

    Comment


    • #3
      Dear Hemanshu :

      Thank you for your help. Sadly your code didn't work (nothing happens at the end) although I do not know why. Indeed, I should have used dataex, but given that a13 is in many file I did not know how to use it properly. One strange thing I noticed is that for one of the file, the following code:

      Code:
      tostring a13, replace
      yields :

      Code:
      a13 has value label; no replace
      but when I try this code:

      Code:
      decode a13, replace
      I get this output :

      Code:
      value label A13 not found
      r(111);
      How can it be possible ?

      Here's an example of my data on one file (the one that yielded the result above)

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str18 country int a12 byte a13
      "Nicaragua" 310 16
      "Nicaragua" 317 16
      "Nicaragua" 303 16
      "Nicaragua" 314 16
      "Nicaragua" 303 16
      "Nicaragua" 309 16
      "Nicaragua" 310 16
      "Nicaragua" 303 16
      "Nicaragua" 309 16
      "Nicaragua" 312 16
      "Nicaragua" 310 16
      "Nicaragua" 317 16
      "Nicaragua" 309 16
      "Nicaragua" 309 16
      "Nicaragua" 318 16
      "Nicaragua" 312 16
      "Nicaragua" 317 16
      "Nicaragua" 314 16
      "Nicaragua" 314 16
      "Nicaragua" 312 16
      "Nicaragua" 311 16
      "Nicaragua" 318 16
      "Nicaragua" 310 16
      "Nicaragua" 312 16
      "Nicaragua" 309 16
      end
      label values a12 A12
      label values a13 A13
      and for another :


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte country str9 a12 str3 a13
      44 "Alca19978" "Rs2"
      44 "Alna78901" "Rs2"
      44 "Alsa21543" "Rs2"
      44 "Alca43215" "Rs2"
      44 "Alsa67890" "Rs2"
      44 "Alna78901" "Rs2"
      44 "Alna78901" "Rs2"
      44 "Alsa67890" "Rs1"
      44 "Alsa67892" "Rs1"
      44 "Alsa67891" "Rs1"
      44 "Alsa67892" "Rs2"
      44 "Alsa67890" "Rs2"
      44 "Alna78901" "Rs2"
      44 "Alna78901" "Rs1"
      44 "Alsa67890" "Rs2"
      44 "Alca19978" "Rs2"
      44 "Alsa67892" "Rs2"
      44 "Altr12345" "Rs2"
      44 "Alna78901" "Rs1"
      44 "Alsa21543" "Rs2"
      44 "Alna78901" "Rs1"
      44 "Altr12345" "Rs2"
      44 "Alca19998" "Rs1"
      44 "Alsa67891" "Rs1"
      44 "Alna78901" "Rs1"
      end
      label values a1 A1
      label def A1 44 "Albania", modify
      EDIT: Apologies, when copying your code I did a typo in the global name which is why the code produced nothing. Rather this is the output i get :

      value label A12 not found
      r(111);

      Last edited by Hugo Denis; 08 Sep 2022, 16:18.

      Comment


      • #4
        Sorry for the double post, but for anyone who might face a similar issue, I could solve it by adding to the first line of the loop provided by Hemanshu

        Code:
        cap label val `appendvar'
        This line of code was suggested by Nick Cox in this following thread:

        https://www.stata.com/statalist/arch.../msg00800.html

        Thank you for everything!

        Comment

        Working...
        X