问题
I have a set of csv files that are very simple to load into Stata using the -insheet- command. But they have very uninformative variable names. For each of these files, I also have a file of metadata consisting of two columns: the original (uninformative) variable names, and a description of what the variables actually mean. I'd like to use these metadata files to create variable labels, preferably without going through and typing up all the separate label commands or turning the metadata file into a dictionary for each file. It seems like there must be a quick way of loading the metadata file into Stata and looping through it to generate the label commands, but I don't know what it is. Any thoughts?
回答1:
Ideally each line of the metadata is something like
varname1 "more interesting description"
in which case you can prefix each line with
label var
and then run the file as if it were a do-file using do
. See the help for label
. That is easy in a decent text editor, as for example searching for the start of each line and replacing it with label var
(note the need for the space).
What could bite here includes:
You don't have double quotes " " as delimiters, in which case you need to insert them.
The extra information does not qualify as a variable label because it is more than 80 characters long. See
help limits
.
There are other ways to do this with Stata. You could write a program to read in the metadata and write out a do-file using file
, but if this were my problem I would reach first for my text editor. (Most experienced Stata programmers use something else as well as doedit
.)
来源:https://stackoverflow.com/questions/14591130/in-stata-how-do-i-add-variable-labels-from-a-separate-csv-file