Hi community,
I want to create a dummy variable with a varlist (two categories, 3 non-continuous waves of construct variables each individually) via forvalues. But it reported "Invalid syntax". The dummy variable is expected to show as a three-section functions with three values. But I failed to do so with a loop function of 'forvalues' firstly. Secondly, when I use a hand-way of 'replace', it only shows the value of one section, but failed to show another two sections.
Could experts help me to check it and figure out a solution for me? Thanks,
//----------- My code is as below" ------------------
//------------ The result is as below: -----------------
. use data.dta
.
end of do-file
. tab r1shlt, m
r1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 5,095 19.98 50.55
1.Excellent | 106 0.42 50.96
2.Very good | 1,146 4.49 55.45
3.Good | 2,274 8.92 64.37
4.Fair | 5,918 23.20 87.57
5.Poor | 3,169 12.43 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. tab s1shlt, m
s1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 6,983 27.38 57.95
1.Excellent | 91 0.36 58.30
2.Very good | 980 3.84 62.15
3.Good | 1,962 7.69 69.84
4.Fair | 5,072 19.89 89.73
5.Poor | 2,620 10.27 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. forvalues i = 1/2,4 {
2. foreach var of varlist r`i'shlt s`i'shlt {
3. tab `var', m
4. replace `var' = 1 if `var' == 5 & !missing(`var')
5. replace `var' = 2 if `var' >= 3 & `var' <= 4 & !missing(`var')
6. replace `var' = 3 if `var' >= 1 & `var' <= 2 & !missing(`var')
7. replace `var' = . if missing(`var')
8. rename `var' r`i'SelfH
9. label define selfh_labl`i' 1 "poor" 2 "fair" 3 "good"
10. label val r`i'SelfH selfh_labl`i'
11. }
12. }
invalid syntax
r(198);
end of do-file
r(198);
//---------- Change my code (below) to spot where the wrong with it -------------------.
[CODE]
tab r1shlt, m
tab s1shlt, m
gen r_r1SelfH = .
encode r1shlt, generate(r1shlt_num)
encode s1shlt, generate(s1shlt_num)
tab r1shlt_num, m
tab s1shlt_num, m
replace r_r1SelfH = 1 if (r1shlt_num == 5 | s1shlt_num ==5) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = 2 if (r1shlt_num == 3/4 | s1shlt_num == 3/4) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = 3 if (r1shlt_num == 1/2 | s1shlt_num == 1/2) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = . if (r1shlt_num == .| s1shlt_num == .)
label define r_r1selfh_labl 1 "poor" 2 "fair" 3 "good"
label val r_r1SelfH r_r1selfh_labl1
tab r_r1SelfH, m
tab r2shlt, m
gen r_r2SelfH = .
encode r2shlt, generate(r2shlt_num)
encode s2shlt, generate(s2shlt_num)
replace r_r2SelfH = 1 if (r2shlt_num == 5 | s2shlt_num ==5) & !missing(r2shlt_num) & !missing(s2shlt_num)
replace r_r2SelfH = 2 if (r2shlt_num == 3/4 | s2shlt_num == 3/4) & !missing(r2shlt_num) & !missing(s2shlt_num)
replace r_r2SelfH = 3 if (r2shlt_num == 1/2 | s2shlt_num == 1/2) & !missing(r2shlt_num) & !missing(s2shlt_num)
label define r_r2selfh_labl 1 "poor" 2 "fair" 3 "good"
label val r_r2SelfH r_r2selfh_labl1
tab r_r2SelfH, m
[CODE]
//--------- It shows unexpected outcome as below ---------------------
. tab r1shlt, m
r1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 5,095 19.98 50.55
1.Excellent | 106 0.42 50.96
2.Very good | 1,146 4.49 55.45
3.Good | 2,274 8.92 64.37
4.Fair | 5,918 23.20 87.57
5.Poor | 3,169 12.43 100.00
------------+-----------------------------------
Total | 25,504 100.00
. tab s1shlt, m
s1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 6,983 27.38 57.95
1.Excellent | 91 0.36 58.30
2.Very good | 980 3.84 62.15
3.Good | 1,962 7.69 69.84
4.Fair | 5,072 19.89 89.73
5.Poor | 2,620 10.27 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
. gen r_r1SelfH = .
(25,504 missing values generated)
. encode r1shlt, generate(r1shlt_num)
. encode s1shlt, generate(s1shlt_num)
. tab r1shlt_num, m
r1shlt_num | Freq. Percent Cum.
------------+-----------------------------------
. | 5,095 19.98 19.98
1.Excellent | 106 0.42 20.39
2.Very good | 1,146 4.49 24.89
3.Good | 2,274 8.92 33.80
4.Fair | 5,918 23.20 57.01
5.Poor | 3,169 12.43 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
. tab s1shlt_num, m
s1shlt_num | Freq. Percent Cum.
------------+-----------------------------------
. | 6,983 27.38 27.38
1.Excellent | 91 0.36 27.74
2.Very good | 980 3.84 31.58
3.Good | 1,962 7.69 39.27
4.Fair | 5,072 19.89 59.16
5.Poor | 2,620 10.27 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. replace r_r1SelfH = 1 if (r1shlt_num == 5 | s1shlt_num ==5) & !missing(r1shlt_num) | !missing
> (s1shlt_num)
(17,708 real changes made)
.
end of do-file
. replace r_r1SelfH = 2 if (r1shlt_num == 3/4 | s1shlt_num == 3/4) & !missing(r1shlt_num) | !mi
> ssing(s1shlt_num)
(17,708 real changes made)
.
end of do-file
. do "C:\Users\ACER\AppData\Local\Temp\STD400c_000000.t mp"
. replace r_r1SelfH = 3 if (r1shlt_num == 1/2 | s1shlt_num == 1/2) & !missing(r1shlt_num) | !mi
> ssing(s1shlt_num)
(17,708 real changes made)
.
end of do-file
. replace r_r1SelfH = . if (r1shlt_num == .| s1shlt_num == .)
(0 real changes made)
.
end of do-file
. label define r_r1selfh_labl 1 "poor" 2 "fair" 3 "good"
. label val r_r1SelfH r_r1selfh_labl1
. tab r_r1SelfH, m
r_r1SelfH | Freq. Percent Cum.
------------+-----------------------------------
3 | 17,708 69.43 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. tab r2shlt, m
r2shlt | Freq. Percent Cum.
------------+-----------------------------------
| 6,892 27.02 27.02
. | 1,021 4.00 31.03
1.Excellent | 241 0.94 31.97
2.Very good | 1,774 6.96 38.93
3.Good | 2,512 9.85 48.78
4.Fair | 9,232 36.20 84.97
5.Poor | 3,832 15.03 100.00
------------+-----------------------------------
Total | 25,504 100.00
. gen r_r2SelfH = .
(25,504 missing values generated)
. encode r2shlt, generate(r2shlt_num)
. encode s2shlt, generate(s2shlt_num)
. replace r_r2SelfH = 1 if (r2shlt_num == 5 | s2shlt_num ==5) & !missing(r2shlt_num) & !missin
> g(s2shlt_num)
(12,714 real changes made)
. replace r_r2SelfH = 2 if (r2shlt_num == 3/4 | s2shlt_num == 3/4) & !missing(r2shlt_num) & !mi
> ssing(s2shlt_num)
(0 real changes made)
. replace r_r2SelfH = 3 if (r2shlt_num == 1/2 | s2shlt_num == 1/2) & !missing(r2shlt_num) & !mi
> ssing(s2shlt_num)
(0 real changes made)
.
. label define r_r2selfh_labl 1 "poor" 2 "fair" 3 "good"
. label val r_r2SelfH r_r2selfh_labl1
.
. tab r_r2SelfH, m
r_r2SelfH | Freq. Percent Cum.
------------+-----------------------------------
1 | 12,714 49.85 49.85
. | 12,790 50.15 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
.
I want to create a dummy variable with a varlist (two categories, 3 non-continuous waves of construct variables each individually) via forvalues. But it reported "Invalid syntax". The dummy variable is expected to show as a three-section functions with three values. But I failed to do so with a loop function of 'forvalues' firstly. Secondly, when I use a hand-way of 'replace', it only shows the value of one section, but failed to show another two sections.
Could experts help me to check it and figure out a solution for me? Thanks,
//----------- My code is as below" ------------------
Code:
use data.dta tab r1shlt, m tab s1shlt, m forvalues i = 1/2,4 { foreach var of varlist r`i'shlt s`i'shlt { tab `var', m replace `var' = 1 if `var' == 5 & !missing(`var') replace `var' = 2 if `var' >= 3 & `var' <= 4 & !missing(`var') replace `var' = 3 if `var' >= 1 & `var' <= 2 & !missing(`var') replace `var' = . if missing(`var') rename `var' r`i'SelfH label define selfh_labl`i' 1 "poor" 2 "fair" 3 "good" label val r`i'SelfH selfh_labl`i' } }
. use data.dta
.
end of do-file
. tab r1shlt, m
r1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 5,095 19.98 50.55
1.Excellent | 106 0.42 50.96
2.Very good | 1,146 4.49 55.45
3.Good | 2,274 8.92 64.37
4.Fair | 5,918 23.20 87.57
5.Poor | 3,169 12.43 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. tab s1shlt, m
s1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 6,983 27.38 57.95
1.Excellent | 91 0.36 58.30
2.Very good | 980 3.84 62.15
3.Good | 1,962 7.69 69.84
4.Fair | 5,072 19.89 89.73
5.Poor | 2,620 10.27 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. forvalues i = 1/2,4 {
2. foreach var of varlist r`i'shlt s`i'shlt {
3. tab `var', m
4. replace `var' = 1 if `var' == 5 & !missing(`var')
5. replace `var' = 2 if `var' >= 3 & `var' <= 4 & !missing(`var')
6. replace `var' = 3 if `var' >= 1 & `var' <= 2 & !missing(`var')
7. replace `var' = . if missing(`var')
8. rename `var' r`i'SelfH
9. label define selfh_labl`i' 1 "poor" 2 "fair" 3 "good"
10. label val r`i'SelfH selfh_labl`i'
11. }
12. }
invalid syntax
r(198);
end of do-file
r(198);
//---------- Change my code (below) to spot where the wrong with it -------------------.
[CODE]
tab r1shlt, m
tab s1shlt, m
gen r_r1SelfH = .
encode r1shlt, generate(r1shlt_num)
encode s1shlt, generate(s1shlt_num)
tab r1shlt_num, m
tab s1shlt_num, m
replace r_r1SelfH = 1 if (r1shlt_num == 5 | s1shlt_num ==5) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = 2 if (r1shlt_num == 3/4 | s1shlt_num == 3/4) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = 3 if (r1shlt_num == 1/2 | s1shlt_num == 1/2) & !missing(r1shlt_num) | !missing(s1shlt_num)
replace r_r1SelfH = . if (r1shlt_num == .| s1shlt_num == .)
label define r_r1selfh_labl 1 "poor" 2 "fair" 3 "good"
label val r_r1SelfH r_r1selfh_labl1
tab r_r1SelfH, m
tab r2shlt, m
gen r_r2SelfH = .
encode r2shlt, generate(r2shlt_num)
encode s2shlt, generate(s2shlt_num)
replace r_r2SelfH = 1 if (r2shlt_num == 5 | s2shlt_num ==5) & !missing(r2shlt_num) & !missing(s2shlt_num)
replace r_r2SelfH = 2 if (r2shlt_num == 3/4 | s2shlt_num == 3/4) & !missing(r2shlt_num) & !missing(s2shlt_num)
replace r_r2SelfH = 3 if (r2shlt_num == 1/2 | s2shlt_num == 1/2) & !missing(r2shlt_num) & !missing(s2shlt_num)
label define r_r2selfh_labl 1 "poor" 2 "fair" 3 "good"
label val r_r2SelfH r_r2selfh_labl1
tab r_r2SelfH, m
[CODE]
//--------- It shows unexpected outcome as below ---------------------
. tab r1shlt, m
r1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 5,095 19.98 50.55
1.Excellent | 106 0.42 50.96
2.Very good | 1,146 4.49 55.45
3.Good | 2,274 8.92 64.37
4.Fair | 5,918 23.20 87.57
5.Poor | 3,169 12.43 100.00
------------+-----------------------------------
Total | 25,504 100.00
. tab s1shlt, m
s1shlt | Freq. Percent Cum.
------------+-----------------------------------
| 7,796 30.57 30.57
. | 6,983 27.38 57.95
1.Excellent | 91 0.36 58.30
2.Very good | 980 3.84 62.15
3.Good | 1,962 7.69 69.84
4.Fair | 5,072 19.89 89.73
5.Poor | 2,620 10.27 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
. gen r_r1SelfH = .
(25,504 missing values generated)
. encode r1shlt, generate(r1shlt_num)
. encode s1shlt, generate(s1shlt_num)
. tab r1shlt_num, m
r1shlt_num | Freq. Percent Cum.
------------+-----------------------------------
. | 5,095 19.98 19.98
1.Excellent | 106 0.42 20.39
2.Very good | 1,146 4.49 24.89
3.Good | 2,274 8.92 33.80
4.Fair | 5,918 23.20 57.01
5.Poor | 3,169 12.43 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
. tab s1shlt_num, m
s1shlt_num | Freq. Percent Cum.
------------+-----------------------------------
. | 6,983 27.38 27.38
1.Excellent | 91 0.36 27.74
2.Very good | 980 3.84 31.58
3.Good | 1,962 7.69 39.27
4.Fair | 5,072 19.89 59.16
5.Poor | 2,620 10.27 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. replace r_r1SelfH = 1 if (r1shlt_num == 5 | s1shlt_num ==5) & !missing(r1shlt_num) | !missing
> (s1shlt_num)
(17,708 real changes made)
.
end of do-file
. replace r_r1SelfH = 2 if (r1shlt_num == 3/4 | s1shlt_num == 3/4) & !missing(r1shlt_num) | !mi
> ssing(s1shlt_num)
(17,708 real changes made)
.
end of do-file
. do "C:\Users\ACER\AppData\Local\Temp\STD400c_000000.t mp"
. replace r_r1SelfH = 3 if (r1shlt_num == 1/2 | s1shlt_num == 1/2) & !missing(r1shlt_num) | !mi
> ssing(s1shlt_num)
(17,708 real changes made)
.
end of do-file
. replace r_r1SelfH = . if (r1shlt_num == .| s1shlt_num == .)
(0 real changes made)
.
end of do-file
. label define r_r1selfh_labl 1 "poor" 2 "fair" 3 "good"
. label val r_r1SelfH r_r1selfh_labl1
. tab r_r1SelfH, m
r_r1SelfH | Freq. Percent Cum.
------------+-----------------------------------
3 | 17,708 69.43 69.43
. | 7,796 30.57 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
. tab r2shlt, m
r2shlt | Freq. Percent Cum.
------------+-----------------------------------
| 6,892 27.02 27.02
. | 1,021 4.00 31.03
1.Excellent | 241 0.94 31.97
2.Very good | 1,774 6.96 38.93
3.Good | 2,512 9.85 48.78
4.Fair | 9,232 36.20 84.97
5.Poor | 3,832 15.03 100.00
------------+-----------------------------------
Total | 25,504 100.00
. gen r_r2SelfH = .
(25,504 missing values generated)
. encode r2shlt, generate(r2shlt_num)
. encode s2shlt, generate(s2shlt_num)
. replace r_r2SelfH = 1 if (r2shlt_num == 5 | s2shlt_num ==5) & !missing(r2shlt_num) & !missin
> g(s2shlt_num)
(12,714 real changes made)
. replace r_r2SelfH = 2 if (r2shlt_num == 3/4 | s2shlt_num == 3/4) & !missing(r2shlt_num) & !mi
> ssing(s2shlt_num)
(0 real changes made)
. replace r_r2SelfH = 3 if (r2shlt_num == 1/2 | s2shlt_num == 1/2) & !missing(r2shlt_num) & !mi
> ssing(s2shlt_num)
(0 real changes made)
.
. label define r_r2selfh_labl 1 "poor" 2 "fair" 3 "good"
. label val r_r2SelfH r_r2selfh_labl1
.
. tab r_r2SelfH, m
r_r2SelfH | Freq. Percent Cum.
------------+-----------------------------------
1 | 12,714 49.85 49.85
. | 12,790 50.15 100.00
------------+-----------------------------------
Total | 25,504 100.00
.
end of do-file
.
Comment