Announcement

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

  • Import variable labels and notes from one stata file to another

    Hi all

    I have two datasets A and B. B is the collapsed version of A. How can I import variables labels and variable notes from A to B?

    Thanks in advance

  • #2
    Something like this:

    Code:
    //    GET A LIST OF ALL VARIABLES IN B
    use B, clear
    unab vbles: _all
    
    //    EXTRACT THE LABELS AND CHARACTERISTICS
    //    OF THOSE VARIABLES IN DATA SET A
    use A, clear
    foreach v of varlist `vbles'  {
        local `v'_lab: var label `v'
        local `v'_charlist: char `v'[]
        foreach c of local `v'_charlist  {
            local `v'_`c': char `v'[`c']
        }
    }
    
    //    RETURN TO B AND APPLY THEM
    use B, clear
    foreach v of varlist `vbles' {
        label var `v' `"``v'_lab'"'
        foreach c of local `v'_charlist {
            char `v'[`c'] `"``v'_`c''"'
        }
    }
    Notes are characteristics, so they will be picked up, along with any other characteristics of these variables, by this code.

    ​​​​​​​This code is not tested and may contain typos, unbalanced quotes or braces, etc.


    Comment


    • #3
      Dear All,

      I am trying to do the same thing as in Clyde's commands above to extract variable labels from one data set into the other. But when I run the right curly, it gives me an error of ' invalid name
      r(198);'.

      I kindly request your guidance on how to close the loop after running all the commands needed in the loop.

      Thanks in advance.

      Comment


      • #4
        "
        But when I run the right curly, it gives me an error of ' invalid name"
        I don't know what you mean when you refer to "run[ing] the right curly." This code should not be run one-line at a time, or even in chunks. The whole thing needs to be run without interruption from beginning to end. If that does not solve your problem, please post back showing the exact and complete code you are running and the exact and complete output, including error messages, that Stata gives you. Do not edit it in any way. Do it by copying from your Results window or log file to your clipboard and then pasting here in the editor, between code delimiters. (See FAQ #12 if you are not familiar with code delimiters.)

        Comment


        • #5
          @Clyde, Thank you for your post, I was actually running one by one which caused the interruption.

          Comment


          • #6
            Also see descsave by Roger Newson for the labels (but perhaps not the notes):
            Code:
            -------------------------------------------------------------------------------------------------------------------------------------------------
            package descsave from http://fmwww.bc.edu/repec/bocode/d
            -------------------------------------------------------------------------------------------------------------------------------------------------
            
            TITLE
                  'DESCSAVE': module to export data set and machine-readable codebook
            
            DESCRIPTION/AUTHOR(S)
                  
                   descsave is an extension of describe, creating up to 2 output
                  data sets. These are a Stata data file with 1 observation per
                  variable and data on the descriptive attributes of each variable
                  (name, storage type, format value label, variable label, and
                  characteristics if specified), and also a do-file, which can be
                  called to reconstruct these descriptive attributes. The do-file
                  created by descsave can be used if the data set has been saved
                  using outsheet and input again using insheet. This can be useful
                  if the user wishes to create a definitive generic spreadsheet
                  version of the data, and to know that the original Stata version
                  can be reconstructed from the definitive generic version.
                  However, descsave can also be used when the user uses
                  parmest,label after a regression command, using xi dummy
                  variables for multilevel factors, and then reconstructs these
                  multilevel factors from the variable label in the parmest output
                  data set, in order to create tables and/or plots of confidence
                  intervals.
                  
                  KW: data manipulation
                  
                  Requires: Stata version 16.0
                  
                  Distribution-Date: 20200409
                  
                  Author: Roger Newson, National Heart and Lung Institute, Imperial College London
                  Support: email [email protected]
                  
            
            INSTALLATION FILES                             (type net install descsave)
                  descsave.ado
                  descsave.sthlp
            -------------------------------------------------------------------------------------------------------------------------------------------------
            (type ssc install descsave to install)
            David Radwin
            Senior Researcher, California Competes
            californiacompetes.org
            Pronouns: He/Him

            Comment

            Working...
            X