Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Numeric variable (in Excel) with some string cases: To destring without loosing the text information


From   "Joseph Coveney" <jcoveney@bigplanet.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Numeric variable (in Excel) with some string cases: To destring without loosing the text information
Date   Mon, 6 Apr 2009 12:06:06 +0900

Xiangping Jia wrote:

I am working on a second-hand data in Excel format. Most of the
variables are numeric while some are strings. For those numeric
variables, some cases were inputted as strings that includes some
texts for explaining the data when conducting the survey .  While I
need numeric information, I want to check those text information one
by one and to look up in details. Dirty work but necessary.

Here is the way I did.
1) In Excel, I add one new obs as the first obs

id var1 var2 var3
    abc abc  abc
1   23   111  119
2   5     91     89
3  1dog   2cat  4dog

2) Infile the excel data into Stata (for just copy them into Stata).
Since all variables are identified as strings, no information lost.

3) In Stata,
verson 10
drop if id==.   //drop the newly added 1st obs
local xlist var1 var2 var3
foreach v of local xlist {
	destring `v', gen(j`v') force    //generate a numeric variable by
dropping those text information
	list id `v' j`v' if `v'~="" & j`v'==.   // check those text cases
	}

PROBLEM. The syntax works well; the variables are destringed. But the
annoy thing is that the results of the last line is NOT displayed.
But when I type variables one by one, for example "list id var1 jvar1
if var1~=""&jvar1==.", the text cases are displayed
     +-------------------+
     | id   var1   jvar1 |
     |-------------------|
  3. |  3    dot       . |
     +-------------------+

Since there are a great number of variables, no way to do it one by
one. Could someone tell me what's wrong with the syntax? Why the last
line does not perform to show the text cases?

--------------------------------------------------------------------------------

The code that you show works for me:  all of the last line's string variables are displayed (see results below).

Joseph Coveney


. clear *

. set more off

. input byte id str4 var1 str4 var2 str4 var3

           id       var1       var2       var3
  1. .    "abc"  "abc"  "abc"
  2. 1    "23"   "111"  "119"
  3. 2     "5"    "91"   "89"
  4. 3  "1dog"  "2cat" "4dog"
  5. end

. list, noobs

  +-------------------------+
  | id   var1   var2   var3 |
  |-------------------------|
  |  .    abc    abc    abc |
  |  1     23    111    119 |
  |  2      5     91     89 |
  |  3   1dog   2cat   4dog |
  +-------------------------+

. // Xiangping Jia's code copied from the Statalist post:
. // verson 10
. drop if id==.   //drop the newly added 1st obs
(1 observation deleted)

. local xlist var1 var2 var3

. foreach v of local xlist {
  2.         destring `v', gen(j`v') force    //generate a numeric variable by
  3. // dropping those text information
.         list id `v' j`v' if `v'~="" & j`v'==.   // check those text cases
  4.         }
var1 contains nonnumeric characters; jvar1 generated as byte
(1 missing value generated)

     +-------------------+
     | id   var1   jvar1 |
     |-------------------|
  3. |  3   1dog       . |
     +-------------------+
var2 contains nonnumeric characters; jvar2 generated as int
(1 missing value generated)

     +-------------------+
     | id   var2   jvar2 |
     |-------------------|
  3. |  3   2cat       . |
     +-------------------+
var3 contains nonnumeric characters; jvar3 generated as int
(1 missing value generated)

     +-------------------+
     | id   var3   jvar3 |
     |-------------------|
  3. |  3   4dog       . |
     +-------------------+

. exit

end of do-file


.



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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