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
Asymmetric predictors in dynamic panel models?Hi all, In a longitudinal analysis, I suspect that the impact of an increase in a predictor variabl…
Is DiD possible for examining effect of a continuous treatment on a continuous outcome among single group dependent sample?Hi Statalist, I'm new to the forum and a novice Stata user. I'm studying the effect of a continuou…
Undesired behavior (or bug): confirm new variableConsider the following examples: Code: clear confirm new variable byte confirm new variable byte i…
how to calculate the loop for variables?I need to calculate the second row variable minus the first row variable, the fourth row variable mi…
Problem graphing by several variablesHello ! My name is Hans Huber and I was wondering if someone could help me making a certain type of…
Subscribe to:
Post Comments (Atom)
0 Response to Looping to create a difference score
Post a Comment