Hello all,
I am working on a project where I need difference scores. I will have a number of difference scores, so typing the command over-and-over will become tedious. I am trying to develop a loop that will provide me with ONLY the difference scores for my variables of interest. I realize there is probably some process that I am overlooking, so I need some assistance, please. I am working with the following code:
** setting up data **
set obs 10000
set seed 40245
*set trace on
gen x1 = rnormal(25, 10)
gen x2 = rnormal(12, 3)
gen x3 = rnormal(10, 5)
gen x4 = rnormal(15, 8)
gen x5 = rnormal(5, 2)
gen x6 = rnormal(3, 1)
** actual difference scores that I desire **
gen d1 = x2 - x1
gen d2 = x4 - x3
gen d3 = x6 - x5
**loop to replicate the difference scores **
local t1 x1 x3 x5
local t2 x2 x4 x6
local counter = 2
foreach v1 of varlist `t1' {
foreach v2 of varlist `t2' {
local ++counter
gen d`counter' = `v2'-`v1'
}
}
sum
drop *
At the moment, the loop provides me every possible combination between `v1' and `v2'. I only want the three. How do I "tame" the loop? Thank you in advance.
Related Posts with Looping to create a difference score
Subtracting constant in panelHello, I am having a panel model where it says that : ΔY= (1-a) + b1yI,t-1 + b2ytI,-1 + Zi,t …
Unstable Stata ResultsDear Stata users, Sorry! I am running a do-file using Stata 17, but the results vary every time I r…
Hetero(varlist) after -craggit-Hi everyone, Could someone please explain the standard errors used in hetero(varlist)? I'm running …
Counting observations in one column based on another columnI have a dataset concerning music that has one column which contains the number of plays the song ha…
How to print numbers of levels in melogit models using esttab?Hello dear statalists: I am using estout from SSC in Stata 17.0 on Windows 10. My goal is to fit a …
Subscribe to:
Post Comments (Atom)
0 Response to Looping to create a difference score
Post a Comment