Announcement

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

  • Converting data information into variable label

    I got a spreadsheet with its first row contains the variable names and second row contains the corresponding descriptions like:
    A B C
    GDP UEM RATE
    gross domestic product unemployment rate one year coupon bond rate
    Importing the data into Stata, we can easily use the option firstrow to tell Stata taking first row as variable names. However, as the second line contains all the detailed description words of that variable, including them into variable label is of great significance. Initally, I wrote the following code:
    Code:
    clear all
    import excel myData.xlsx, firstrow
    foreach var of varlist *{
          label variable `var' `var'[1]
    }
    drop in 1 
    foreach var of varlist*{
          destring `var', replace
    }
    save myData.dta
    But Stata told me there was a "invalid syntax" and I modified the forth line with a double quotation:
    Code:
    label variable `var' "`var'[1]"
    Unfortunately, all the variables' labels became "varname[1]" form. So I checked help file of label and then I realized that "label variable varname labeltext" command just treats labeltext as a string whatever with a double quotation or not, it seems that reference is inavailable.
    Is there any solution to this problem? Converting information into label merely by keyboard entering is demanding! Thanks!

  • #2
    Code:
    foreach var of varlist *{
            label variable `var' "`=`var'[1]'"
    }
    Strange animal that you have on your profile picture. I have never seen one of those before.

    Comment


    • #3
      Thank you very much! You have completely solved my problem! Its name is capybara, the largest rodent in the world from South America, very cute ^_^

      Comment


      • #4
        Thanks, I will be on the lookout whenever I travel to that part of the world. Agreed, cute!
        Last edited by Andrew Musau; 03 Mar 2020, 08:48.

        Comment

        Working...
        X