Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: ST: labelling again


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: ST: labelling again
Date   Mon, 15 Sep 2003 11:12:24 +0100

Eric Uslaner
>
> Yesterday I posted a query that Nick Cox kindly answered:
>
> >
> > I have an Excel spreadsheet (someone else created it) with
> > three worksheets.  Two have data sets that I have
> > transferred by cutting and pasting into Stata and merging.
> > The third has four columns:
> >
> > Column 1:  variable name data set 1
> > Column 2: variable label data set 1
> > Column 3: variable name data set 2
> > Column 4: variable label data set 2
> >
> > Is there a way to link the variable names and labels (by
> > cutting and pasting?) into the merged data set without
> > having to retype each of the labels?  I presume that you
> > can't do all of this at once, if at all, but column by
> > column would be great.
>
> I think you need to import the names and
> labels into the data set to which they apply.
>
> Let's suppose your variables to be renamed
> are in <varlist> and their proper names are in -ericsnames-
> and their proper variable labels are in -ericslabels-:
>
> local i = 1
> foreach v of var <varlist> {
>     label var `v' `"`= ericslabels[`i']'"'
>     rename `v' `= ericsnames[`i++']'
> }
>
> I'm assuming that the left-to-right order of
> variables maps onto the top-to-bottom order of names
> and labels.
>
>
> However, I am unsure of what I need to do.  I now did a cut
> a paste from Excel and have a list of variable names and
> variable labels.  But they are in (an otherwise empty)
> Stata data set labelled var1 and var2.  Call this data set
> datasetlabels.   The variables are in a data set called
> dataset.  How do I merge the two together?
>
> Flying blind, I merged the two data sets and typed in, with
> the result:
>
> local i = 1
>
> . foreach v of var <varlist> {
>   2. label var `v' `"`= var2[`i']'"'
>   3. rename `v' `= var1[`i++']'
>   4. }
> < invalid name
>
> There is clearly something I am not understanding at a
> fundamental level.  And I think it has to do with how to
> put the original data set together with the "data set"
> containing just the labels and the variable names (since
> the two data sets have different numbers of observations).

Stata's not going to like "<varlist>". You have to say what
your own varlist is.

I start with the raw data

. l

     +--------------+
     | v1   v2   v3 |
     |--------------|
  1. | 14   26   28 |
  2. | 65    5   12 |
  3. | 56   43   41 |
  4. | 61   90   73 |
  5. | 69   53   88 |
     |--------------|
  6. | 11   85   47 |
  7. | 62   22   43 |
  8. |  7   57   90 |
  9. | 56   27    6 |
 10. | 88   95   68 |
     +--------------+

and then I add in the names and the labels:

. l

     +--------------------------------------------------------+
     | v1   v2   v3   names                            labels |
     |--------------------------------------------------------|
  1. | 14   26   28   frog   This variable contains frog data |
  2. | 65    5   12   toad          This one is about toads.  |
  3. | 56   43   41   newt               Newts in this column |
  4. | 61   90   73                                           |
  5. | 69   53   88                                           |
     |--------------------------------------------------------|
  6. | 11   85   47                                           |
  7. | 62   22   43                                           |
  8. |  7   57   90                                           |
  9. | 56   27    6                                           |
 10. | 88   95   68                                           |
     +--------------------------------------------------------+

. local i = 1

. foreach v of var v? {
  2. label var `v' `"`=labels[`i']'"'
  3. rename `v' `= names[`i++']'
  4. }

. l

     +---------------------------------------------------------------+
     | frog   toad   newt   names                             labels |
     |---------------------------------------------------------------|
  1. |   14     26     28    frog   This variable contains frog data |
  2. |   65      5     12    toad          This one is about toads.  |
  3. |   56     43     41    newt               Newts in this column |
  4. |   61     90     73                                            |
  5. |   69     53     88                                            |
     |---------------------------------------------------------------|
  6. |   11     85     47                                            |
  7. |   62     22     43                                            |
  8. |    7     57     90                                            |
  9. |   56     27      6                                            |
 10. |   88     95     68                                            |
     +---------------------------------------------------------------+

. d

Contains data
  obs:            10
 vars:             5
 size:           430 (99.9% of memory free)
----------------------------------------------------------------------
---------
              storage  display     value
variable name   type   format      label      variable label
----------------------------------------------------------------------
---------
frog            byte   %8.0g                  This variable contains
frog data
toad            byte   %8.0g                  This one is about toads.
newt            byte   %8.0g                  Newts in this column
names           str4   %9s
labels          str32  %32s
----------------------------------------------------------------------
---------
Sorted by:
     Note:  dataset has changed since last saved

Nick
[email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index