Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: alphabet range

From   Bernie Hogan <>
To   statalist <>
Subject   st: alphabet range
Date   Tue, 28 Sep 2010 13:27:06 +0100

Hi everyone,

I have dyadic data, with many matrix questions (i.e. ranges of similar
questions for both people).

The data are structured like so
q[number][letter][f/s]  such as


where 17 is the matrix, a is the row, and f is the first person to
respond, s is the second.

I would like to be able to do calculations on the entire row. The best
I could come up with was:

local vlist a b c d e f g h i

foreach var of local vlist {
 	gen dsq17`var' = q17`var'f - q17`var's

But that means every time I do a new matrix I have to figure out how
many variables, and then type out their letter by hand.

What I would really like is to be able to say:

foreach var of q17? [f or s] {
gen newvar = q17?f - q17?s
(or whatever is the closest to that)

…but I'm having trouble taking the varname and replacing it with
another character and evaluating them together or creating
programmatically a range of characters. Any suggestions on how to
clean up that code?

Take care,

Dr Bernie Hogan
Research Fellow, Oxford Internet Institute, University of Oxford

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index