Announcement

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

  • Macros and Labeling a large dataset

    I have a dataset with 169 variables,(primary dataset) but the data is corrupted so i had to imported the data directly from Main Database (Secondary dataset) and now i want to label the Secondary dataset by saving the labels in primary dataset (Both of them have the same variable names). I want create a macro where in i want to put in the variable /label combination extracted from Primary Dataset and run it in Secondary Dataset (loops) so that all the variables in the Secondary Dataset will get labeled similarily to primary dataset.
    Last edited by Nachiketh Soodana Prakash; 15 Oct 2014, 07:22.

  • #2
    instead of a macro, why not just use the -label save- command (see -h label-) which puts what you want into a do file that you can then use

    Comment


    • #3
      On the term macro within and without Stata, please see http://www.stata.com/statalist/archi.../msg01258.html

      Comment


      • #4
        Rich Goldstein's suggestion is right on track, assuming that Nachiketh is referring to value labels. But if variable labels are intended, it will be necessary to write a loop something like this:

        Code:
        use primary_dataset, clear
        unab vnames: _all
        //  COLLECT THE VARIABLE LABELS IN LOCAL MACROS
        foreach v of varlist _all {
            local `v'_lab: var label `v'
        }
        
        use secondary_dataset, clear
        foreach v of varlist `vnames' {
            label var `v' `"``v'_lab'"'
        }
        This code assumes that all of the variables that occur in the primary data set also occur in the secondary one. If not, the second -foreach- loop will break because not everything in `vnames' will be a variable name in the current data. If that is the situation, it will be necessary to collect the variable names in the secondary_data set in another local macro, and then replace vnames by the intersection of vnames and the current list of names. (See help macrolists).

        Comment


        • #5
          If the variables in the first dataset do not all occur in the second dataset this fix should suffice:

          Code:
          foreach v in `vnames' {    
              capture label var `v' `"``v'_lab'"'
          }
          The differences are that we don't claim that the names are a varlist, because Stata will check that before the loop is entered; and that we capture any error within the loop.

          Comment


          • #6
            Really appreciate the help. Thanks guys.
            I am just referring to labels not value labels.

            Code:
            v of varlist _all {
            local `v'_lab: var label `v'
            }
            macro list
            The macro list does not show the new macros i created. Is there is something wrong i am doing?
            Last edited by Nachiketh Soodana Prakash; 15 Oct 2014, 13:45.

            Comment


            • #7
              v of varlist _all {local `v'_lab: var label `v' } macro list
              consists of a command fragment, a short code block, and another command, all mashed together on a single line. I'm surprised you got anything at all from this other than some kind of error message about an unrecognized command v.

              Do you mean
              Code:
              foreach v of varlist _all {
                  local `v'_lab: var label `v'
              }
              macro dir
              ?

              That should generate all the new macros and then list them out when you are done. Remember that if you are working from a do-file you have to run all of this code as a block. If you just run the -foreach- loop by itself and then run -macro dir-, the newly created macros will go out of scope and their contents lost. So to get this to work, you must run all of these commands without interruption.

              Comment


              • #8
                I ran the code but it didn't work out for me. Attached is a screenshot(after running the code) i cant see any labels.

                Code:
                use "C:\Users\nxs294\Desktop\Database Files\Cystectomy\dta\Main Cystectomy.dta",clear
                unab vnames: _all
                foreach v of varlist _all{
                 local `v' _lab: var label `v'
                 }
                use "C:\Users\nxs294\Desktop\Database Files\Cystectomy\dta\Main Cystectomy 1.dta",clear
                foreach v of varlist `vnames'{
                label var `v' `"``v'_lab'"'
                }

                Comment


                • #9
                  Your screenshot is not readable (they almost never are and really shouldn't be used in this Forum). Please re-run the code and post the results by copying and pasting the results from the Stata Results window into a code block. To show what's going on in the variable labels, you can just add a describe command to the end of your code.

                  Also, I am skeptical that the code in your last post is what you actually ran, because

                  Code:
                  foreach v of varlist _all{
                  will produce a syntax error due to the lack of a space between _all and {. (It will say "{ required".) Your second -foreach- command has the same problem.

                  If you paste the results from the Results window into a code block, that will also include the exact code you ran, so we will be absolutely sure of what we are dealing with.

                  I can't emphasize enough that we can't help you if you don't follow those instructions: when you re-type your code, you can (and I think do) make subtle changes to it that may be important. Do not re-type your code. Paste from the Results window into a code block.
                  Last edited by Clyde Schechter; 16 Oct 2014, 09:52.

                  Comment


                  • #10
                    One more point that may be of more immediate help. Looking closely at the code you posted:

                    Code:
                    local `v' _lab: var label `v'
                    If that is in fact the code you ran (as above, I'm skeptical that what you posted is what you actually ran) you will not get the variable labels, because the inclusion of a space between `v' and _lab will not result in creation of a local macro `v'_lab. Rather it will create a local macro `v' whose contents are "_lab: var label `v'" When you then get to the second foreach loop, Stata will look for a macro named `v'_lab (which you reference in your -label var- command) and, finding none, it will assume you mean the empty string, so it will label all your variables with empty strings!

                    Comment


                    • #11
                      Got it. Yes indeed my mistake was leaving the space between `v' and _lab. And i did retype the code earlier just to understand what exactly i am doing with each command so that i can learn better. I actually ran that code it didn't give me errors but there was no result . Well about the screenshot i didn't know it would decrease the pixel. I really appreciate the suggestions and the quick solutions. Finally got it to work. Thanks again.
                      Last edited by Nachiketh Soodana Prakash; 16 Oct 2014, 11:44.

                      Comment

                      Working...
                      X