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
How to remove an extra digit/character from a string variableDear Statalist users I have a dataset which has a string variable in three parts, by convention, se…
Grouping multiple entries for a distinct id in longitudinal dataDear all, This might be a trivial question, however, I am posting as I have searched multiple stata…
xtmerlogitFriends, I had this query; I am fitting three levels logistic-mixed effect model with Random coeff…
Linux: Make Stata default application of file type .dta (etc.)How can I change the default application of file types such as .dta, .do, .gph, .smcl to Stata? I su…
How to generate predicted value by beta/standardized regression coefficients, rather than unstandardized ones?Is there a simple way to generate predicted value by beta/standardized regression coefficients. For …
Subscribe to:
Post Comments (Atom)
0 Response to Looping to create a difference score
Post a Comment