Statalist


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

RE: st: Re: -encode- help..


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Re: -encode- help..
Date   Wed, 19 Nov 2008 21:16:56 -0000

I agree with Martin. Here's a quick stab at it. 

*! 1.0.0 NJC 19 Nov 2008 
program mencode
	version 8 
	syntax varlist(string) [if] [in] , stub(str) [ label(str) ] 

	if index("`stub'", "@") == 0 { 
			di as err "stub must contain @" 
			exit 198
	}
	
	// test the variable names 
	foreach v of local varlist { 
		local new : subinstr local stub "@" "`v'" 
		confirm new var `new'
	} 

	marksample touse, strok 
	qui count if `touse' 
	if r(N) == 0 error 2000 

	if "`label'" == "" local label "`: word 1 of `varlist''"
	
	// do it 
	foreach v of local varlist { 
		local new : subinstr local stub "@" "`v'" 
		encode `v' if `touse', gen(`new') label(`label') 
		qui compress `new' 
	} 
end 

Comments: 

-mencode- works on a string varlist. 

You may specify -if- or -in-. 

You must specify a -stub()-. The stub must include the character @,
which means the present varname. You should add a prefix or suffix or
both. So if your stub is "n@", the new variable names will be prefixed
by "n". -mencode- checks first that the new names implied will be OK. 

You may specify a name for the new value labels. If you don't, -mencode-
will use the name of the first variable you specify.  	

Here's an example: 

. l var?

     +---------------------------+
     | var1   var2   var3   var4 |
     |---------------------------|
  1. |    a      b      c      d |
  2. |    a      b      c      d |
  3. |    a      b      c      d |
  4. |    a      b      c      d |
     +---------------------------+

. mencode var?, stub(n@)

. l var? nvar?

     +-----------------------------------------------------------+
     | var1   var2   var3   var4   nvar1   nvar2   nvar3   nvar4 |
     |-----------------------------------------------------------|
  1. |    a      b      c      d       a       b       c       d |
  2. |    a      b      c      d       a       b       c       d |
  3. |    a      b      c      d       a       b       c       d |
  4. |    a      b      c      d       a       b       c       d |
     +-----------------------------------------------------------+

. l var? nvar?, nola

     +-----------------------------------------------------------+
     | var1   var2   var3   var4   nvar1   nvar2   nvar3   nvar4 |
     |-----------------------------------------------------------|
  1. |    a      b      c      d       1       2       3       4 |
  2. |    a      b      c      d       1       2       3       4 |
  3. |    a      b      c      d       1       2       3       4 |
  4. |    a      b      c      d       1       2       3       4 |
     +-----------------------------------------------------------+

Martin Weiss

Nick`s contribution makes me think that it is possible to automate this
in 
the fashion that you describe...

moleps islon

> So apparently no easy solution to this. The perfect solution would be
> a command that accepted a varlist, automatically generated new
> variables concatenating the old variablename with a userspecified
> _name_ and labeled the values according to a predefined labelset...
> That would also let the user set the startnumber for the codes...
> Gotta learn programming:-)
>
>>>> _______________________
>>>> ----- Original Message ----- From: "moleps islon"
<moleps2@gmail.com>
>>>> To: <statalist@hsphsun2.harvard.edu>
>>>> Sent: Wednesday, November 19, 2008 8:30 PM
>>>> Subject: st: -encode- help..
>>>>
>>>>
>>>>> I've got 30 different text variables that all have the same
possible
>>>>> values. Is there an easy way to encode all 30 variables using the
same
>>>>> label or do I have to do it manually. Also is it possible,
somehow, to
>>>>> specify stata to start encoding with tha value 0 instead of 1 ?

*
*   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