在Stata中,如何从单独的csv文件添加变量标签? [英] In Stata, how do I add variable labels from a separate csv file?

查看:2129
本文介绍了在Stata中,如何从单独的csv文件添加变量标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组csv文件,使用-insheet-命令加载到Stata非常简单。但它们有非常不知情的变量名。对于每个这些文件,我还有一个元数据文件,包括两列:原始(无信息)变量名称,以及变量实际意义的描述。我想使用这些元数据文件来创建变量标签,最好不经过并打开所有单独的标签命令或将元数据文件转换为每个文件的字典。似乎必须有一个快速的方法加载元数据文件到Stata和循环通过它生成标签命令,但我不知道它是什么。任何想法?

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?

推荐答案

理想情况下,元数据的每一行都像

Ideally each line of the metadata is something like

varname1more interesting description

varname1 "more interesting description"

在这种情况下,您可以在每行前加上

in which case you can prefix each line with

label var 

然后运行该文件,就像它是一个do-文件使用 do 。请参阅标签的帮助。这在一个相当不错的文本编辑器中很容易,例如搜索每行的开始,并替换为 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. 有双引号作为分隔符,在这种情况下,您需要插入它们。

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

额外信息不能作为变量标签,因为它超过80个字符。请参阅帮助限制

The extra information does not qualify as a variable label because it is more than 80 characters long. See help limits.

这与Stata。你可以写一个程序读取元数据,并使用 file 写出一个do文件,但如果这是我的问题,我会达到我的文本编辑器。 (最有经验的Stata程序员使用别的东西以及 doedit 。)

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

这篇关于在Stata中,如何从单独的csv文件添加变量标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆