Statalist


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

Re: st: using vector notation to simplify coding


From   "Michael I. Lichter" <mlichter@buffalo.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: using vector notation to simplify coding
Date   Tue, 11 Aug 2009 12:19:13 -0400

Dan,

Your ing_* and mes_* variables can easily be handled with macros in loops. You don't need variables def_* because they're constants. The real trouble is that you essentially need to do a lookup in each calculation to get the right value of def_* given the value of mes_*. We brute force our way around the lookup by looping through the 12 possibilities (borrowing from p. 192 of Nick Cox's indispensible Stata Journal article "Problems with Lists" http://www.stata-journal.com/article.html?article=pr0009):

// this first part just creates the variables
clear
set obs 100
// six observations per case with an income and month number for each obs
forvalues i = 1/6 {
   gen ing_`i' = 100000 * runiform()
   gen mes_`i' = round((runiform()*(6-1))+1)
}

// a loop for each of the 6 observations per case
forvalues t = 1/6 {
   // a loop for each of 12 months
   forvalues i = 1/12 {
// the list on the left should be the list of deflation factors, not month numbers
       local def : word `i' of 1 2 3 4 5 6 7 8 9 10 11 12
       replace ing_`t' = ing_`t' / `def' if mes_`t' == `i'
   }
}
exit

It would be more elegant if there were a way to do a lookup like this:

       replace ing_`t' = ing_`t' / DEF[mes_`t']

where DEF was a vector corresponding to the list of deflation factors. As far as I know, that's not possible.

HTH

Michael

Dan Waldo wrote:
Dear Statalist members,

I have data in monthly expenditures in variables ing_1 through ing_6, and survey months in variables mes_1 through mes_6 (these vary from respondent to respondent). I wish to adjust the monthly expenditures for inflation, and I have deflation factors in variables def_1 through def_12.

I am pretty accomplished in SAS, but only a novice in Stata. In SAS I would say:

array def {12} def_1 - def_12;
array ing {6} ing_1 - ing_6;
array mes {6} mes_1 - mes_6;
do t=1 to 6;
   month=mes{t};
   ing{t}=ing{t}/def{month};
   end;

I have been searching unsuccessfully for the Stata analogue to this; I am sure it is written somewhere and I am just not using the best search terms.

Will somebody please point me to the proper place?

Thanks!

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com *
*   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/

--
Michael I. Lichter, Ph.D. <mlichter@buffalo.edu>
Research Assistant Professor & NRSA Fellow
UB Department of Family Medicine / Primary Care Research Institute
UB Clinical Center, 462 Grider Street, Buffalo, NY 14215
Office: CC 126 / Phone: 716-898-4751 / FAX: 716-898-3536

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