Announcement

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

  • Variable and labels transfer & Analysing mutliple variables at once

    Hi,

    I am new to Stata, appreciate your advice on the below:

    1) How do I transfer my variable and label assignments and properties from Dataset A to Dataset B which I will be uploading, is very similar to Dataset A except for a few additional columns?
    - I tried saving them in a do-file, but it doesn't work when I upload Dataset B and execute the do-file with my variable and label assignments.

    2) How do I get the mean & standard deviation of age for the subgroups of my sample?

    Thank you!

  • #2
    I tried saving them in a do-file, but it doesn't work when I upload Dataset B and execute the do-file with my variable and label assignments.
    Putting them in a do-file is the way to do this. But saying that something "doesn't work" is really not helpful--there are so many ways that something might not work, and it is impossible to guess in which particular way your efforts failed. A more detailed explanation of what happened and what went wrong is needed. In fact, when asking for help with code, it is usually wise to show example data (from both data sets) and the actual code you tried, along with the output you got from Stata. And, since there is no such thing as a "minor" change in code, the code and results should be shown exactly, by copy/pasting from your do-file(s) and the Results window. To show example data in the most helpful way, use the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    How do I get the mean & standard deviation of age for the subgroups of my sample?
    The question is, again, too vague to respond to. Which subgroups--how do you distinguish the subgroups in your data set? And how do you want these results--just displayed in the Results window? Or do you need new variables in the data set that show them in the observations of the corresponding subgroups? Or do you want to store them in local macros or scalars? Or something else?

    Comment


    • #3
      Hi Clyde,

      Many thanks for your detailed reply.

      My Excel A consists of 157 variables which i have imported to Stata and edited all their labels (data labels, variable labels, value labels)
      Thereafter, I added a few more columns to my Excel, resulting in 161 variables in my Excel B.
      I would like to import Excel B to Stata, and transfer all my labels from Excel A to Excel B, so that I don't have to re-edit the same variable and value labels and only need to edit the labels for the additional variables.

      I have saved a *.do file with the label commands from my Dataset A (i.e. Excel A), see example below:
      lab var language "English/ Mandarin"
      lab var retest "Retest done? "

      After importing Dataset B (i.e. Excel B) to Stata, and excuting the above commands, all labels in Dataset B were not updated according to the commands above. The results window shows:
      . lab var language "English/ Mandarin"
      variable language not found
      r(111);
      .
      . lab var retest "Retest done? "
      variable retest not found
      r(111);

      Apologies, I am still figuring out how to use the dataex function to represent my data better.




      Comment


      • #4
        In this particular circumstance, where you are adding four additional variables to the file, there is probably an easier way. For this method to work, there must be some variable or set of variables that (jointly) identify unique observations in the data set. For example, in panel data sets, the panel identifier and time variable do this. Sometimes just a single ID variable can do the job. It depends on what your data is, but there is usually something.

        If so, keep your first Stata file as is. Let's say it's name is from_excel_A.dta. Now, import Excel B into a Stata data file. Let's call it from_excel_B.dta. Now -merge- the two data sets as follows:
        Code:
        user from_excel_A, clear
        merge 1:1 identifying_variable(s) using from_excel_B
        This will leave everything in the original from_excel_A file exactly as it was, including all of the labeling. It will simply add the new variables to the data set. Then you can, if appropriate, put finishing touches like labels on the new variables, and then save the combined file.

        Comment


        • #5
          Thank you very much!

          Comment

          Working...
          X