In Stata, how do I add variable labels from a separate csv file?

对着背影说爱祢 提交于 2019-12-22 12:19:22

问题


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:

  1. You don't have double quotes " " as delimiters, in which case you need to insert them.

  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!