Hello,
I'm currently working on a project that requires me to extract a large amount of values from a matrix the current format is as follows:
The variables I want to extract are integers that represent a purchase date for items. so for instance Var 4 would take the value of 0, 1, 2 or 3 depending on the period they were purchased.
However, I dont need every variable in the dataset. Furthermore, the data format I require for my algorithm is as follows.
In essence the second column would take the value of the period 1, 2 or 3 depending on when it was purchased and the third column would be the item identifier which was the row name.
So far the only solution I could came up with was to go through each entry one at a time and recode into a matrix. However this is time consuming and not a best practice as far as i know. Below is a sample code:
I have looked into some solutions but can't figure this one out with the existing stata functions. Could someone point me towards the right direction ?
Thank you.
I'm currently working on a project that requires me to extract a large amount of values from a matrix the current format is as follows:
| ID | Var 1 | Var 2 | Var 3 | Var 4 | Var 5 | ... | Var N |
| 1 | String | Int | Int | Int | Int | ... | other |
| 2 | String | Int | Int | Int | Int | ... | other |
| 3 | String | Int | Int | Int | Int | ... | other |
However, I dont need every variable in the dataset. Furthermore, the data format I require for my algorithm is as follows.
| ID | Value | Var |
| 1 | Int | Var 3 |
| 1 | Int | Var 4 |
| 1 | Int | Var 5 |
| 2 | Int | Var 3 |
| 2 | Int | Var 5 |
| 2 | Int | Var 7 |
| 2 | Int | Var 8 |
| 3 | Int | Var 4 |
| 3 | Int | Var 5 |
In essence the second column would take the value of the period 1, 2 or 3 depending on when it was purchased and the third column would be the item identifier which was the row name.
So far the only solution I could came up with was to go through each entry one at a time and recode into a matrix. However this is time consuming and not a best practice as far as i know. Below is a sample code:
Code:
//Q8 -- Fill in the created matrix variables
local num = 8
local varlist "a b c d e"
local i = 1
local j = 1
gen Q`num'all=""
while `i'<=118679 {
if IndexQ`num'[`i'] ==1 {
foreach x of local varlist {
if Q`num'`x'[`i'] ==1 {
local let`j' = "`x'"
local j = `j' + 1
}
}
replace Q`num'all = "`let1'" in `i'
}
if IndexQ`num'[`i'] ==2 {
foreach x of local varlist {
if Q`num'`x'[`i'] ==1 {
local let`j' = "`x'"
local j = `j' + 1
}
}
replace Q`num'all = "`let1',`let2'" in `i' // new variable to be created for transaction mode
}
if IndexQ`num'[`i'] ==3 {
foreach x of local varlist {
if Q`num'`x'[`i'] ==1 {
local let`j' = "`x'"
local j = `j' + 1
}
}
replace Q`num'all = "`let1',`let2',`let3'" in `i'
}
if IndexQ`num'[`i'] ==4 {
foreach x of local varlist {
if Q`num'`x'[`i'] ==1 {
local let`j' = "`x'"
local j = `j' + 1
}
}
replace Q`num'all = "`let1',`let2',`let3',`let4'" in `i'
}
if IndexQ`num'[`i'] ==5 {
foreach x of local varlist {
if Q`num'`x'[`i'] ==1 {
local let`j' = "`x'"
local j = `j' + 1
}
}
replace Q`num'all = "`let1',`let2',`let3',`let4',`let5'" in `i'
}
local i = `i'+1
local j=1
}
Thank you.

Comment