Announcement

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

  • Label Variables

    Hello,

    I have a stata file named "survey.dta" which contains the following labelled data:

    identifiant educ gender region income
    11111 primary female rural 800
    11112 secondary male urban 900
    11113 primary male rural 900
    11114 university female rural 1000


    This file is accompanied with the following excel file named "variables.xls" which contains the question related to the variables in "survey.dta":


    Variable name Value label Variable label
    identifiant Farmers' ID
    educ educ What is your highest level of education?
    gender gender Farmers' gender
    region region Where do you mostly live for your activities?
    income What is the total value of your production?


    I would like to label variables in the first file (i.e., "survey.dta") using variable labels from the second file (i.e., "variables.xls") in Stata 13. For example, for the identifiant, I can write the following code manually:


    label var identifiant "Farmers' ID"


    But I have 2K variables in my data and would like to write a do-file that will help me to do all of that at once.


    NB: The values of the variables contained in the first file "survey.dta" are already labelled.


    I will be grateful for any comments and suggestions.


    Best regards,
    Antoine
    Last edited by Antoine Dedewanou; 16 Feb 2020, 05:24.

  • #2
    If you import your second file to Stata, then you can have string variables like this. You can then write do files and in due course export each variable, something like this.

    Code:
    . list 
    
         +------------------------------------------------------------------------+
         | variablen~e   valuel~l                                   variablelabel |
         |------------------------------------------------------------------------|
      1. | identifiant                                                Farmers' ID |
      2. |        educ       educ        What is your highest level of education? |
      3. |      gender     gender                                 Farmers' gender |
      4. |      region     region   Where do you mostly live for your activities? |
      5. |      income                What is the total value of your production? |
         |------------------------------------------------------------------------|
    
    . gen wanted1 = "label var " + variablename + `" ""' + variablelabel + `"""'
    
    . l wanted1
    
         +------------------------------------------------------------------+
         |                                                          wanted1 |
         |------------------------------------------------------------------|
      1. |                              label var identifiant "Farmers' ID" |
      2. |        label var educ "What is your highest level of education?" |
      3. |                               label var gender "Farmers' gender" |
      4. | label var region "Where do you mostly live for your activities?" |
      5. |   label var income "What is the total value of your production?" |
         |------------------------------------------------------------------|
    
    . gen wanted2 = "label val " + variablename + " " + valuelabel if valuelabel != ""

    Comment


    • #3
      Thank you Nick for your reply and your suggestion. However, I don't see the rest of your code. After generating wanted1 and wanted2, how can I label the variable contained in my first data file named "survey.dta"?
      Last edited by Antoine Dedewanou; 16 Feb 2020, 06:03.

      Comment


      • #4
        There is no more code of mine beyond what I have mentioned. The text for each do file is within a string variable, which you should export. If you look at the listing of wanted1, wanted2 you can see the do files in embryo. Then run your do files on the main dataset.

        Comment


        • #5
          Let me spell out the remainder of Nick's clever approach.

          Code:
          rename wanted1 display
          tempfile tmp
          outfile wanted1 using "`tmp'" , noquote
          use survey.dta , clear
          do "`tmp'"
          There are other ways of doing what I did above.

          Best
          Daniel

          Comment


          • #6
            Thank you so much Daniel and Nick. It works now.

            Comment

            Working...
            X