I do not know why my code did not work but I guess I did something the
function string() is not designed for. Instead I tried something different
with word() and that works perfectly:
**********************
use data1, clear
qui ds
local x "`r(varlist)'"
qui d
local y "`r(k)'"
di `y'
set obs `y'
gen str61 typ = ""
tokenize "`x'"
forvalues no = 1/`y' {
di "`1'"
replace typ = word("`1'",1) in `no'
mac shift
}
**********************
Johannes
----------------------
Johannes Geyer
Deutsches Institut f�r Wirtschaftsforschung (DIW Berlin)
German Institute for Economic Research
Department of Public Economics
DIW Berlin
Mohrenstra�e 58
10117 Berlin
Tel: +49-30-89789-258
Johannes Geyer <[email protected]>
Gesendet von: [email protected]
26/02/2008 11:21
Bitte antworten an
[email protected]
An
[email protected]
Kopie
Thema
Re: st: converting a local of variable names in string variables
Thank you Sarah! I was not aware of -sxpose- which is a nice tool.
However, it does not exactly do what I am looking for. When I transpose
the data I loose their names - the option firstnames does something else.
I still try to convert variable names to variables which I thought to be a
less confusing problem. When I first used -sxpose- I had dropped all
observations before. This resulted in the error message
"sxpose would create new variables _var1-_var0, but names already in use"
and this was caused by having no observations to be transposed.
Once again, I tried the following:
****************
use data1.dta, clear /* no observations, only variable names */
qui ds
local x "`r(varlist)'"
qui d
local y = `r(k)'
di `y'
set obs `y'
gen str61 typ = ""
forvalues no = 1/`y' {
local n = `n' + 1
local Q: word `n' of `x'
di "`Q'"
replace typ = string(`Q') in `n'
}
****************
what it does is the following: the variable -typ- contains missing values
since I dropped all observations and Stata simply looks for data entries
in variables. However, all I wanted is one variable with variable names as
observations.
I fear and hope that I make it too complicated - do you have any
suggestion?
Thank you,
Johannes
----------------------
Johannes Geyer
Deutsches Institut f�r Wirtschaftsforschung (DIW Berlin)
German Institute for Economic Research
Department of Public Economics
DIW Berlin
Mohrenstra�e 58
10117 Berlin
Tel: +49-30-89789-258
"Sarah Cohodes" <[email protected]>
Gesendet von: [email protected]
25/02/2008 16:52
Bitte antworten an
[email protected]
An
[email protected]
Kopie
Thema
Re: st: converting a local of variable names in string variables
Hi Johannes,
Try the user installed module -sxpose- by Nick Cox (see:
http://econpapers.repec.org/software/bocbocode/s446402.htm) if you
want to transpose your dataset with string variables. To install,
type:
ssc install sxpose
Then you can merge on your ID using -merge-; it doesn't matter if the
identification variable you use is string or not.
Good luck,
Sarah
On Mon, Feb 25, 2008 at 10:14 AM, Johannes Geyer <[email protected]> wrote:
> Dear Statalist,
>
> I am trying to match a data set of variable idenfifiers (data1) and a
data
> set that consists of these variables (data2).
>
> data1:
>
> var id v1 v2 v3
> 1 jk12 kk14 lk16
> 2 jk24 kk67 lk90
> 3 jk16
> ..
>
> data2:
>
> obs id jk12 jk24 jk16
> 1 12000 56 1
> 2 12000 71 0
> 3 ...
> ..
>
> now I need to have a procedure that links variable names from data2 with
a
> subset "var id" from data1. My idea was to save variable names in a
local
> macro, then generate string variables from it and merge it to data1 -
> however, I am not used to strings and I do not know if that is possible.
>
> I tried the following
>
> ******************
> #d;
> use data2.dta, clear;
> qui ds;
> local x "`r(varlist)'";
> qui d;
> local y "`r(k)'";
> di `y';
>
> forvalues no = 1/`y' {;
> gen str61 typ`no' = "";
> local n = `n' + 1;
> local Q: word `n' of `x';
> replace typ`no' = string(`Q');
> };
> ******************
> This ends with the content (not the names) of variables copied as string
> in the new variables - but this is not what I need, I need names only.
>
> I would like to proceed by using the index to reshape and then merge it
to
> the other data set. With export and manipulation by hand I might be able
> to solve this problem, but I prefer to have it in Stata.
>
> I appreciate any suggestion on that,
>
> thank you
>
> Johannes
>
>
>
>
>
> ----------------------
> Johannes Geyer
> Deutsches Institut f�r Wirtschaftsforschung (DIW Berlin)
> German Institute for Economic Research
> Department of Public Economics
> DIW Berlin
> Mohrenstra�e 58
> 10117 Berlin
> Tel: +49-30-89789-258
>
> *
> * For searches and help try:
> * http://www.stata.com/support/faqs/res/findit.html
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/