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 inputvars.

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: Automating generation of new vars from coordinated groups of inputvars.
Date   Thu, 17 Nov 2005 23:21:44 -0500

Something like this ought to work (haven't tested it yet):

foreach var of varlist *yr* {
local prefix=substr("`var'",1,index("`var'","yr")-1)
local suffix=substr("`var'",index("`var'","yr")+2,.)
gen int `prefix'dt`suffix'=mdy(`prefix'mo`suffix',`prefix'day`suffix',`var')
format *dt* %d

This approach will work with all vars that have yr somewhere in their name. If there are other variables that meet the *yr* spec that you don't want this done for, then it would be better to list out the *yr* variables that you want in the foreach line

Michael Blasnik

----- Original Message ----- From: "Clare L Maxwell" <>
To: <>
Sent: Thursday, November 17, 2005 10:23 PM
Subject: st: Automating generation of new vars from coordinated groups of input vars.

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.

Yours truly,
Clare Maxwell
*   For searches and help try:

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