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]

st: RE: How do I create a loop that calculates new variables by performing the simple mathematical function of subtracting one varlist on another varlist?


From   "Philip Ryan" <[email protected]>
To   <[email protected]>
Subject   st: RE: How do I create a loop that calculates new variables by performing the simple mathematical function of subtracting one varlist on another varlist?
Date   Mon, 9 Jan 2012 13:58:56 +1030

<<>>

I think the only potential problem here is the zero left padding of the
numeric suffix of the variable names.

Try this:

forvalues i=1/73 {

local suffix cond(`i' < 10,"00`i'","0`i'")

gen   h7a`suffix' = h7r`suffix' - h7t`suffix'
 
}




Phil


Philip Ryan
Professor and Director
Data Management & Analysis Centre [DMAC]
University of Adelaide 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Saporito,
Salvatore J
Sent: Monday, 9 January 2012 1:18 PM
To: [email protected]
Subject: st: How do I create a loop that calculates new variables by
performing the simple mathematical function of subtracting one varlist on
another varlist?

I am trying to subtract one sequential list of variables (e.g., a1 to a73)
from another sequential list (e.g., b1 to b73) using a loop statement such
as the "foreach..." I am trying to avoid repeating the same, simple function
(in this case 73 times).

Specifically, I want a loop that repeats this:

gen h7a001=h7r001-h7t001
gen h7a002=h7r001-h7t002
to
gen h7a073=h7r003-h7t073

Simple question but I am trying to learn Stata so that I can use the power
of its programming capabilities. This particular challenge eludes me.

Thanks, 

Sal

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

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index