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

st: RE: Automating generation of new vars from coordinated groups of input vars.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Automating generation of new vars from coordinated groups of input vars.
Date   Fri, 18 Nov 2005 04:19:46 -0000

I have the impression that you have bashed your head
against this several times, and that waving previous versions
of the problem at the list did not help. 

The key question is thus why does this appear difficult? 
It is, I guess, mostly because of unsystematic naming. So, I wouldn't 
start from here or try to end up where you are aiming. 

To simulate your example problem I produced a little dataset: 

clear 
set obs 1

local dvar "vday bday hivday cd4day rnday hxday1 hxday2 bgday1 bgday2 endday1 endday2 lpday1 lpday2 cbday1 cbday2 ceday1 ceday2"
local mvar "vmo bmo  hivmo  cd4mo rnmo hxmo1 hxmo2 bgmo1 bgmo2 endmo1 endmo2 lpmo1 lpmo2 cbmo1 cbmo2 cemo1 cemo2"
local yvar "vyr byr  hivyr  cd4yr rnyr hxyr1 hxyr2 bgyr1 bgyr2 endyr1 endyr2 lpyr1 lpyr2 cbyr1 cbyr2 ceyr1 ceyr2" 

foreach v in `dvar' `mvar' { 
	gen `v' = 1 
}
foreach v in `yvar' { 
	gen `v' = 2000 
} 

ds 

The problem needs to be cleaned up before it gets worse. 

foreach v in `dvar' `mvar' `yvar' { 
	local V : subinstr local v "day1" "1day"
	local V : subinstr local V "day2" "2day"
	local V : subinstr local V "mo1" "1mo"
	local V : subinstr local V "mo2" "2mo" 
	local V : subinstr local V "yr1" "1yr" 
	local V : subinstr local V "yr2" "2yr" 
	capture rename `v' `V' 
} 

ds 

Now you can make a list of the prefixes: 

unab dvar : *day
local var : subinstr local dvar "day" "", all 

and then each date is just 

foreach v of local var { 
	gen `v'date = mdy(`v'mo,`v'day,`v'yr) 
} 

The moral is: Fix the problem upstream. 

Also, check out -renvars-. 

Nick 
[email protected] 

Clare L Maxwell
 
> I have lots of dates I would like to calculate from month, day and 
> year variables.  It is a repetative task, and I would like to find a 
> way to automate it so that I would not have to write a gen command 
> for each group of variables.  Below is a partial list:
> 
> "day" variables:  vday bday hivday cd4day rnday
>                hxday1 hxday2 bgday1 bgday2 endday1 endday2
>                lpday1 lpday2 cbday1 cbday2 ceday1 ceday2
> "month" variables: vmo bmo  hivmo  cd4mo rnmo
>                hxmo1 hxmo2 bgmo1 bgmo2 endmo1 endmo2
>                lpmo1 lpmo2 cbmo1 cbmo2 cemo1 cemo2
> "year" variables: vyr byr  hivyr  cd4yr rnyr
>                hxyr1 hxyr2 bgyr1 bgyr2 endyr1 endyr2
>                lpyr1 lpyr2 cbyr1 cbyr2 ceyr1 ceyr2
> new "date" variables:  vdate bdate hivdt cd4dt rndt
>                hxdt1 hxdt2 bgdt1 bgdt2 enddt1 enddt2
>                lpdt1 lpdt2 cbdt1 cbdt2 cedt1 cedt2
> 
> Goal:  compute dates from all these month, day and year variables 
> without writing a gen command for each set.
> 
> I have more complicated repetative tasks that also use coordinated 
> groups of variables.  This seemed simplest to explain.  I hope if I 
> find a way to deal with the dates, I will be able to generalize it to 
> similar repetative situations.
> 
> Thank you very much in advance for your help.

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