有一些不確定性(這意味着,缺乏信息)有關您的變量是不是字符串或數字和什麼是什麼對齊,你不給變量名。因此,這缺乏一個最小的,完整的和可驗證的例子。見https://stackoverflow.com/help/mcve但這種技術可能會有所幫助。我不會顯式地循環觀察(不是行:Stata不是電子表格)。
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 v1 str7 v2 str5 v3 str3(v4 v5) str4 v6 str2 v7
"ZARA." "cotton." "35." "28." "27." "350." "0"
"" "125." "3200." "1." "5." "32." "41"
"Mango." "nylon." "35." "27." "28." "320." "0"
"" "125." "3050." "1." "5." "33." "40"
end
quietly forval j = 8/13 {
local i = `j' - 6
gen v`j' = v`i'[_n+1] if mod(_n, 2) == 1
}
drop if mod(_n, 2) == 0
list
+---------------------------------------------------------------------------+
1. | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 | v9 | v10 | v11 |
| ZARA. | cotton. | 35. | 28. | 27. | 350. | 0 | 125. | 3200. | 1. | 5. |
|---------------------------------------------------------------------------|
| v12 | v13 |
| 32. | 41 |
+---------------------------------------------------------------------------+
+---------------------------------------------------------------------------+
2. | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 | v9 | v10 | v11 |
| Mango. | nylon. | 35. | 27. | 28. | 320. | 0 | 125. | 3050. | 1. | 5. |
|---------------------------------------------------------------------------|
| v12 | v13 |
| 33. | 40 |
+---------------------------------------------------------------------------+
這裏
_n
是運行1個向上觀察數。
mod(_n, 2)
是將觀測值除以2得到的餘數,因此奇數爲1,偶數爲0。
您可能想要destring
什麼是字符串,但應該是數字。
編輯這裏有另一種方法來做到這一點,但它取決於所有變量是字符串。
clear
input str6 v1 str7 v2 str5 v3 str3(v4 v5) str4 v6 str2 v7
"ZARA." "cotton." "35." "28." "27." "350." "0"
"" "125." "3200." "1." "5." "32." "41"
"Mango." "nylon." "35." "27." "28." "320." "0"
"" "125." "3050." "1." "5." "33." "40"
end
gen long id = _n
gen odd = mod(_n, 2)
reshape long v, i(id) j(varno)
replace id = ceil(id/2)
replace varno = varno + 6 if !odd
drop if missing(v)
drop odd
reshape wide v, i(id) j(varno)
拼寫是Stata。 30多年來它一直不是STATA。 –