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: How do I create a loop that calculates new variables by performing the simple mathematical function of subtracting one varlist on another varlist?
From
James Sams <[email protected]>
To
[email protected]
Subject
Re: st: 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, 09 Jan 2012 00:39:36 -0600
On Monday 09, January 2012 02:47:44 you wrote:
> 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.
My preferred solution is to use display as an extended function
. local i 1
. display "`i'"
1
. local j : display %03.0f `i'
. display "`j'"
001
so at the top of your loop, reassign your loop variable, or perhaps make a new
variable This is more general than the previously posted solution.
--
James Sams
[email protected]
*
* 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/