I'm trying to figure out a command way to save myself some steps in a .do file, and was hoping that someone could help me out.
Here's the setup: I use a particular dataset pretty regularly that is in wide form, but I need it in long form for my analyses. Because of the way the data are coded, I need to rename all the variables before I reshape the data (to get the "j" component to the end).
Because of this, I have a series of three commands that are each referring to the same set of variables -- but in each instance it is referring them in different ways (because they're being renamed in the process).
What I'd like is a way to refer to the strings that are common in all three sets of commands, but that I am able to add a prefix to each variable in one line of command, a suffix in the second, and just the string in the 3rd.
So here is a simplified example of what I'm doing.
Let's say I have two variables (alpha and beta), each measures at three time points (1, 2, and 3). Thus, in wide there a six variables:
Code:
v1alpha v2alpha v3alpha v1beta v2beta v3beta
The first step is move the "v" and the time portion to the end of the variable name:
Code:
forvalues i=1/3{ foreach x of varlist v`i'alpha v`i'beta { capture renvars `x', postf(_v`i') capture renvars `x', predrop(2) } }
Code:
reshape long alpha_v beta_v, i(id) j(wave)
Code:
foreach x in alpha_v beta_v { renvars `x'_v, postdrop(2) renvars `x', pref(v) }
Because I'm actually using a much larger list of variables, and that these change both within and across projects, I'd like to be able to identify the strings in one line of command before my three steps, and then just refer back to that contents stored in that command line for each of my three steps. So, something like this:
Code:
local stringsare alpha beta // In this instance, I need it to add "v`i'" to the beginning each string forvalues i=1/3{ foreach x of varlist `stringsare' { capture renvars `x', postf(_v`i') capture renvars `x', predrop(2) } } // In this instance, I need it to add "v`i'" to the end each string reshape long `stringsare', i(id) j(wave) // In this instance, I need it to just refer to each string foreach x in `stringsare' { renvars `x'_v, postdrop(2) renvars `x', pref(v) }
Kind regards,
blk
0 Response to line of strings to reduce programming steps
Post a Comment