[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: converting a local of variable names in string variables

From   Johannes Geyer <>
Subject   st: converting a local of variable names in string variables
Date   Mon, 25 Feb 2008 16:14:09 +0100

Dear Statalist,

I am trying to match a data set of variable idenfifiers (data1) and a data 
set that consists of these variables (data2).


var id  v1      v2      v3
1       jk12    kk14    lk16
2       jk24    kk67    lk90
3       jk16


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

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 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:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index