0
votes

I would like to create a variable that takes a name of a value in particular cell. For example my data set looks like this

var1 count
xx   1
xc   2
xv   3
xj   4

I would like to create 4 new variables that take names from the values of the variable var1. For example, three variables would be xx xc xv xj. I understand reshape would do this but in my case I don't want to use reshape. I tried the code below

forvalues i =1/4{
local d var1  count ==`i'
gen xx_`d'= . 
}
1
Very unclear. What would these variables contain? What is the relevance of the count to your question? Your code would fail, if only because second time round the loop the variable xx_d already exists. Check out help separate.Nick Cox
Why 4/8? Relevance to example? Is counter supposed to be the same as count?Nick Cox
@NickCox Sorry for the confusion and the mistakes in my post. Firstly the variables will contain arbitrary values. Secondly count is calculated using _n. Hope this clears the confusion Rodrigo
Sorry, that clarifies little for me. "will contain arbitrary values" could hardly be more obscure. Count calculated using _n: means nothing to me either. I don't get what you want.Nick Cox
@NickCox for now I want the variable values to be .. I would like to create a set of variables that would have names == values of var1. The actual value of each created variable should be ==. Rodrigo

1 Answers

0
votes

This will fail unless the variable looked at, here var1, is a string variable and every distinct value of the variable used could be a new and legal variable name. I've not tried to make the code resistant to failures of the assumptions.

levelsof var1, local(levels) 
foreach v of local levels { 
    gen `v' = . 
} 

It's not the question, but it's hard to see how this could be useful.