0
votes

I'm learning how to write a program in Stata for the first time and I'm having difficulty generalising my program so I can parse an arbitrary list of variables when renaming variables in datasets.

I'm working with two datasets. First one is panel dataset containing the life satisfaction of interviewees in a survey over a duration of 26 years (same dataset as in my previous question). The variables are originally named in this format: ap6801 bp9301 cp9601 and all the way to zp15701. ap6801 contains the respondents' life satisfaction for the year 1985, bp9301 contains it for 1986, and so on.

I wrote the following program to rename the variables so instead of ap6801 it would be lsat1985.

program myprogram 
local mcode 1984
foreach stub in a b c d e f g h i j k l m n o p q r s t u v w x y z {
    local mcode = `mcode' + 1
    rename `stub'* lsat`mcode'
}

Now, I want to modify and generalise this program so that I can use it on my second dataset and with arbitrary numbers. The second dataset consists of variables abetto bbetto cbetto all the way until zbetto. These variables indicate whether a specific person has been interviewed in a specific year and, if not, why not. abetto corresponds to year 1985, bbetto corresponds to year 1986, and so on.

My goal is to write a generalised version of the program so that when I enter an arbitrary list of variables and other information (for example lsat, and a list of numbers (eg: 1985-2010)): myprogram ap6801-zp15701 , the variables will be renamed lsat1985 lsat1986... lsat2010

I'm guessing the program will have the following basic structure:

program myprogram
syntax varlist
foreach x of varlist {
}

Within the loop, there might be local letter = substr("`x'",1,1) to identify the first letter of the variables (a, b, c, d...). Next step would be to link the letters of the alphabet to numbers that will be specified by the user, and a rename command that renames the variable in the format: lsat/betto year. I'm having a difficult time putting all that together in code.

I'm new to Stata and programming, so any help is appreciated!

1

1 Answers

2
votes

Your program only works in your extraordinary circumstances. (Trivially, there is no end statement.) It will fail if any of the wildcards a* b* and so on does not occur as corresponding variable names in the data. Other way round, each rename is from a wildcard, say a*, to one variable name, say lsat1985, and this will work if and only if there is precisely one variable in each case.

More generally, this is an example of premature programming. Prejudice alert: It's not good style to write programs for such highly specific tasks, wiring in a specific variable name prefix, and for such highly unusual circumstances. At most, this is territory for code in a do-file. But if you allow variable name abbreviation, this should work. Good style here implies explaining the circumstances.

* each wildcard a* b* ... is matched by a single variable
set varabbrev on 
local mcode = 1985 
foreach pre in `c(alpha)' { 
    rename `pre' lsat`mcode'
    local ++mcode 
} 

Note that there is no need to type out all the lower case letters a to z. Stata holds all those in one place as c(alpha). You are not expected to know that. For completeness, and this may be irrelevant to your problem, note that Stata variable names can start with other characters, most notably underscore _. I am left wondering about data for years from 2011 on.

It's hard not to write this without running the risk of seeming obnoxiously patronising. And the commentary may seem entirely unfair, as your goal is precisely that of generalising the program! However, in Stata it is often a better idea to write do-files first and move to a program when, and only when, you have used a do-file in so many different circumstances that the need for a more general program becomes evident. So, I won't answer your question on a more general program. It's not obviously a good idea and even if it were it is hard for an outsider to know what that program should look like. You have given one example where the suffix looks unpredictable (6801 9301 9601 and so on) and one where it is predictable (betto) and we can have no idea what else is true (unless freakishly someone here recognises your dataset). A program written for a dataset that is just 26 variables asomething to zsomething is possible, but would you ever use it more than a few times?