Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

RE: st: Wishlist for Stata 13


From   "Hoffman, George" <ghoffman@mcw.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Wishlist for Stata 13
Date   Wed, 30 Jan 2013 15:08:15 +0000

I will add to the wish for a 'defv' feature; I wondered about a 'set defv on' switch that would tell stata to update the list of variable definitions with each generate or replace command. However, it seems stata is quite insistent about strict use of generate for new variables and replace for existing, and I've become quite lazy using defv, not being forced to differentiate. And that does create problems.

For what it's worth, I also wrote an update to defv named 'def' that allows egen and by(sort); the syntax is:

 def [by] [egen] : var1 = exp [if exp] [in range]
 def [bys] [egen] : var1 = exp [if exp] [in range]

(the program is modified from Rich Goldstein's updated version and all credit is due him)

--------------------------------------------------
*!  Version 1.1.4 <06Mar1999>  STB-51 dm50.1 mod gmh for egen
program define def
*!  Define (generate or replace) variables and record their definition
*!  Syntax:
*!    . def [by varlist:] [type] newvar [: lblname] = exp [if exp] [in range]
*!    . def [by varlist:] oldvar = exp [if exp] [in range] [, nopromote]
*!    . def oldvar ?
**  Author:  John R. Gleason (loesljrg@accucom.net)
**  modified by:  George M Hoffman

   version 6.0

   global d_V "note"
*  to record definitions so that they are distinct from notes,
*  remove the asterisk (*) from the next line:
*   global d_V "defV"

   if `"`1'"' == "?" {
      which def
      exit
   }
   if `"`2'"' == "?" {
      xx `1'
      exit
   }
   
   tokenize `"`0'"', parse(":")
   if "`2'" == ":" {
      local 0 `"`3'"'
      local B `1'`2'
   }
   
   local eq = index(`"`0'"', "=")
   if substr(`"`0'"', `eq'-1, 1) == " " { local eq = `eq' - 1 }
   local sp = index(`"`0'"', " ")
   if `sp' >= `eq'  { local sp 1 }
   local V = trim(substr(`"`0'"', `sp', `eq'-`sp'))

   local op "`B' generate"
   qui capture confirm new var `V'
   if _rc == 110 {
      local op "`B' replace"
      local j : char `V'[${d_V}0]
   }
   else if _rc { error _rc }
   local j = cond("`j'" == "", 1, `j'+1)
   
   if index(`"`0'"', "egen") > 0 {
	  qui cap drop `V'
	  local op "`B' "
	  }
	
   nobreak {
      `op' `0'
      char `V'[${d_V}0] `j'
      char `V'[${d_V}`j'] `op' `0'
   }
end


program define xx
   local varlist "req ex"
   parse `"`*'"'

   local j : char `1'[${d_V}0]
   if "`j'" != "" {
      di in ye "`1':"
      local i 1
      while `i' <= `j' {
         local b : char `1'[${d_V}`i']
         if `"`b'"' != "" { di in gr %4.0g "  `i'.", `"`b'"' }
         local i = `i' + 1
      }
   }
end
---------------------------------------------------------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Richard Goldstein
Sent: Wednesday, January 30, 2013 7:56 AM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Wishlist for Stata 13

Thank you -- I will certainly try these and report issues, if any

Rich

On 1/30/13 8:50 AM, Maarten Buis wrote:
> On Wed, Jan 30, 2013 at 2:17 PM, Richard Goldstein wrote:
>> a big (to me) caveat to the -defv- solution (and also to Maarten Buis'
>> solution from today): it does not work with -egen- and it does not 
>> work with -by-
> 
> Here are the solutions:
> 
> *! version 1.0.0 30Jan2013 MLB
> program define myegen, byable(onecall)
> 	if "`_byvars'" != "" {
> 		local byby "by `_byvars' : "
> 	}
> 	`byby'egen `0'
> 
> 	// find the name of variable
> 	gettoken x rest : 0
> 	local type "byte int long float double"
> 	if `: list x in type' {
> 		gettoken x : rest
> 	}
> 	gettoken x : x, parse("=")
> 
> 	// add note
> 	note `x' : `byby'egen `0'
> end
> 
> *! version 1.0.0 30Jan2013 MLB
> program define mygen, byable(onecall)
> 	if "`_byvars'" != "" {
> 		local byby "by `_byvars' : "
> 	}
> 	`byby'generate `0'
> 
> 	// find the name of variable
> 	gettoken x rest : 0
> 	local type "byte int long float double"
> 	if `: list x in type' {
> 		gettoken x : rest
> 	}
> 	gettoken x : x, parse("=")
> 	gettoken x : x, parse(":")
> 
> 	// add note
> 	note `x' : `byby'generate `0'
> end
> 
> *! version 1.0.0 30Jan2013 MLB
> 	program define myreplace, byable(onecall)
> 	if "`_byvars'" != "" {
> 		local byby "by `_byvars' : "
> 	}
> 	`byby'replace `0'
> 
> 	// find the name of variable
> 	gettoken x rest : 0
> 	local type "byte int long float double"
> 	if `: list x in type' {
> 		gettoken x : rest
> 	}
> 	gettoken x : x, parse("=")
> 
> 	// add note
> 	note `x' : `byby'replace `0'
> end
> 
> Hope this helps,
> Maarten
> 
> ---------------------------------
> Maarten L. Buis
> WZB
> Reichpietschufer 50
> 10785 Berlin
> Germany
> 
> http://www.maartenbuis.nl
> ---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index