Announcement

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

  • Invalid variable name and how to deal with?

    Hello,
    I am working on a dataset that consists of a key string variable (MAST chapter) that refers to trade policy eg. (sanitary and phytosanitary measures). I need to reshape my data set and count every one of them. My problem is how to name the new variables, I need to rename them because they have space, etc.. I use commands such as (following commands work very well)

    replace MASTchapter = strtoname(MASTchapter)
    or
    replace MASTchapter = subinstr(MASTchapter, " ", "", .)

    to turn them into workable names is Stata. However, when I want to reshape my dataset by

    reshape wide count, i(id) j(MASTchapter) string
    rename count* *

    STATA returns

    countA_Sanitary_and_phytosanitary_m invalid variable name


    I know that these names are very long (more than 31 characters) and I need to modify them. Since my dataset is very large, I am looking for a smart way to do it other than just replacing every one of them with new names. Is there any solution to this problem?

    Thanks for the support.


  • #2
    Perhaps use encode to turn the string variable into a numeric one, then obtain the variables count1, count2, count3....?

    Comment


    • #3
      Thanks for your message. Since the names are attributed to specific policies in international trade, I have to keep going with comprehensible names for further analysis.

      Comment


      • #4
        your data setup is extremely unclear to me; please follow the advice in the FAQ and post an example using -dataex- and CODE blocks

        Comment


        • #5
          Originally asked at https://www.statalist.org/forums/for...in-my-data-set, and answered there.

          Note: At that thread, O.P. was asked to start a new thread because this question is off topic for the earlier thread. I responded at the old thread because I did not notice this new post at the time.

          Comment

          Working...
          X