Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Variable names and labels for interaction terms
From
Maarten buis <[email protected]>
To
[email protected]
Subject
Re: st: Variable names and labels for interaction terms
Date
Wed, 23 Mar 2011 08:16:47 +0000 (GMT)
--- On Wed, 23/3/11, Nic wrote:
> I've figured out how to create all my (continuous x
> continuous) interaction variables at once but I'm trying to
> learn how to (1) automatically create variable labels for
> them, and (2) name the new variables based on truncated
> versions of the component variables' names.
In the example below I used an extended macro function to
collect the variable label of a variable. You can read more
about extended macro functions by typing in Stata -help macro-
and follow the link to extended macro functions.
I also used the functions -min()- and -substr()-, and you
can read more about those in -help functions-.
*--------------------- begin example --------------------------
sysuse nlsw88, clear
foreach s of varlist s* {
// collect variable label
local labs : variable label `s'
// names is the first three characters of s, unless
// s consists of less than 3 characters
local l = min(`:length local s', 3)
local names = substr("`s'", 1, `l')
foreach t of varlist t* {
// same as above, but than for t
local labt : variable label `t'
local l = min(`:length local t', 3)
local namet = substr("`t'", 1, `l')
// create variable
gen `names'X`namet' = `s'*`t'
// attach variable labels
label variable `names'X`namet' "`labs' X `labt'"
}
}
// see the result
desc s* t*
*------------------------- end example --------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/