Hi,
I'm new to Stata, so I'm still struggling with even the easier concepts of Stata. At this point I'm really just learning commands but I know that my original method is probably unnecessarily long.
I've imported data from a .csv document and somehow it puts nearly all my data in as string variables. Besides that, this particular survey is (partly) coded reversed. So in my orignal code, the five items that should form a scale have to be generated into a numeric variable, which in turn has to be reverse coded into a new variable so that I can run the reliability tests etc. etc. (7 point likert)
My current method:
gen wrsmu_1 = real(q9_1)
gen wrsmu_2 = real(q9_2)
gen wrsmu_3 = real(q9_3)
gen wrsmu_4 = real(q9_4)
gen wrsmu_5 = real(q9_5)
gen wrsmu_1n=8-wrsmu_1
gen wrsmu_2n=8-wrsmu_2
gen wrsmu_3n=8-wrsmu_3
gen wrsmu_4n=8-wrsmu_4
gen wrsmu_5n=8-wrsmu_5
This is my first attempt at foreach loops, so perhaps I'm doing something very obviously wrong, but when looking at help files on stata.com this is what I came up with (and generates code r(198) invalid name). My guess is that it can't generate a new name since 'var'b isn't a known command, but how do I solve this?
foreach var in varlist q9* {
gen 'var'b=8-real('var')
}
Sander
I'm new to Stata, so I'm still struggling with even the easier concepts of Stata. At this point I'm really just learning commands but I know that my original method is probably unnecessarily long.
I've imported data from a .csv document and somehow it puts nearly all my data in as string variables. Besides that, this particular survey is (partly) coded reversed. So in my orignal code, the five items that should form a scale have to be generated into a numeric variable, which in turn has to be reverse coded into a new variable so that I can run the reliability tests etc. etc. (7 point likert)
My current method:
gen wrsmu_1 = real(q9_1)
gen wrsmu_2 = real(q9_2)
gen wrsmu_3 = real(q9_3)
gen wrsmu_4 = real(q9_4)
gen wrsmu_5 = real(q9_5)
gen wrsmu_1n=8-wrsmu_1
gen wrsmu_2n=8-wrsmu_2
gen wrsmu_3n=8-wrsmu_3
gen wrsmu_4n=8-wrsmu_4
gen wrsmu_5n=8-wrsmu_5
This is my first attempt at foreach loops, so perhaps I'm doing something very obviously wrong, but when looking at help files on stata.com this is what I came up with (and generates code r(198) invalid name). My guess is that it can't generate a new name since 'var'b isn't a known command, but how do I solve this?
foreach var in varlist q9* {
gen 'var'b=8-real('var')
}
Sander
Comment