简体   繁体   English

在Stata文件列表中重命名和创建变量

[英]Renaming and creating variables in a list of Stata files

I have a list of Stata datasets: among some a variable tor is absent, and I want to add that variable if it doesn't exist. 我有一个Stata数据集的列表:在其中一些不存在变量tor ,并且如果该变量不存在,我想添加该变量。

The datasets contain a variable called x class where x could be anything (eg Aclass , lclass , etc.). 数据集包含一个名为x class的变量,其中x可以是任何值(例如Aclasslclass等)。 I would like to rename those variables to dec . 我想rename这些变量renamedec

I want to create a variable adjusted which is "yes" if the file name contains adjusted and "no" if not. 我想创建一个变量adjusted"yes" ,如果文件名中包含adjusted"no"如果不是。

I guess it would look something like: 我想看起来像这样:

Loop through list of datasets and their variables {
        if variable contains pattern class 
                        rename to dec
        if no variable tor, then 
                        gen str tor = total
        if file name contains pattern adjusted
                        gen str adjusted = yes
        else gen str adjusted = no
}

But then in proper Stata language. 但是然后使用适当的Stata语言。

So I've got this now, but it's not working, it doesn't do anything... 所以我现在有了这个,但是它不起作用,它什么也没做...

cd "C:\Users\test"
local filelist: dir "." files "*.dta", respectcase

foreach filename of local myfilelist {


   ds *class
     local found `r(varlist)' 
     local nfound : word count `found' 
     if `nfound' == 1 { 
        rename `found' dec
     } 
     else if `nfound' > 1 { 
        di as err "warning: multiple *class variables in `filename'" 
     } 

     capture confirm var tor 
     if !_rc == 0 { 
        gen tor = "total"
     } 

     gen adjusted = cond(strpos("`filename'", "_adjusted_"), "yes", "no") 
}

This is not an answer, this is advice that won't fit into a comment. 这不是答案,这是不适合发表评论的建议。

What you are attempting is not elementary Stata. 您尝试的不是基本的Stata。 If indeed you are unfamiliar with Stata (not stata) you will find it challenging to automate this process. 如果确实不熟悉Stata(而不是stata),您将发现自动化此过程非常困难。 I'm sympathetic to you as a new user of Stata - it's a lot to absorb. 作为Stata的新用户,我很同情您-需要吸收很多东西。 And even worse if perhaps you are under pressure to produce some output quickly. 甚至更糟糕的是,如果您承受着迅速产生一些产出的压力。 Nevertheless, I'd like to encourage you to take a step back from your immediate tasks. 不过,我想鼓励您从当前的任务中退后一步。

When I began using Stata in a serious way, I started by reading my way through the Getting Started with Stata manual relevant to my setup. 当我开始认真使用Stata时,我首先阅读了我的设置相关的Stata入门手册。 Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide , and I worked my way through much of that reading as well. 然后,第18章提供了进一步的建议阅读,其中许多内容都在《 Stata用户指南》中 ,我也通过大量阅读来努力。 There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change. 有很多示例可以复制并粘贴到Stata的do-file编辑器中以运行自己,更好的是尝试更改选项以查看结果如何变化。

All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. 所有这些手册都以PDF的形式包含在Stata安装程序中(从11版开始),并且可以从Stata内部进行访问-例如,通过Stata的“帮助”菜单的“ PDF文档”部分。 The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax. 读书的目的并不是要掌握Stata,而是要确保我熟悉各种重要的基本技术,以便在需要时我会回忆起它们的存在,如果没有的话完整语法。

The Stata documentation is really exemplary - there's just a lot of it. Stata文档确实具有示范性-仅是很多。 The path I followed surfaces the things you need to know to get started in a hurry. 我所走的道路浮出水面,这是您急着要入门所需了解的东西。

With that said, you will perhaps find the foreach command helpful for looping, the filelist command for obtaining a list of Stata datasets (not databases), and the ds command for obtaining a list of variable names within a Stata dataset. 话虽如此,您也许会发现foreach命令对循环有用, filelist命令用于获取Stata数据集(而非数据库)的列表,以及ds命令用于获取Stata数据集内的变量名的列表。 More subtly, the capture command will let you attempt to generate your tor variable and will simply fail gracefully if it already exists, saving a small amount of program logic. 更巧妙地, capture命令将使您尝试generate tor变量,并且如果该变量已经存在,将仅会优雅地失败,从而节省了少量程序逻辑。

The middle part can be sketched: 可以画出中间部分:

    // assumes local macro filename contains file name 

    ds *class
    local found `r(varlist)' 
    local nfound : word count `found' 
    if `nfound' == 1 { 
        rename `found' dec 
    } 
    else if `nfound' > 1 { 
        di as err "warning: multiple *class variables in `filename'" 
    } 

    capture confirm var tor 
    if _rc { 
        gen tor = "total"
    } 

    gen adjusted = cond(strpos("`filename'", "adjusted"), "yes", "no") 

On managing lists of files: filelist (SSC) is very good; 关于管理文件列表: filelist (SSC)非常好; also see fs (SSC) for a different approach. 另请参见fs (SSC),以了解其他方法。

EDIT: Here is proof of concept for the last detail: 编辑:这是最后一个细节的概念证明:

. local filename1 "something adjusted somehow"

. local filename2 "frog toad newt dragon"

. di cond(strpos("`filename1'", "adjusted"), "yes", "no")
yes

. di cond(strpos("`filename2'", "adjusted"), "yes", "no")
no

strpos("<string1>", "<string2>") returns a non-zero result, namely the starting position of the second string in the first if the first contains the second. strpos("<string1>", "<string2>")返回一个非零的结果,即如果第一个字符串包含第二个字符串,则第二个字符串在第一个字符串中的起始位置。 Non-zero as an argument means true in Stata; 非零自变量在Stata中表示true; zero means false. 零表示错误。

See help strpos() and if desired help cond() . 请参阅help strpos()以及需要的help cond()

I can't see your filenames to comment or test your code, but one possible problem is that the local macro is not defined in the same namespace as that in which you are trying to evaluate the expression. 我看不到要注释或测试代码的文件名,但是一个可能的问题是,本地宏未在与您尝试评估表达式的名称空间相同的名称空间中定义。 (That's what local means.) A macro that isn't defined will be evaluated as an empty string, with the result you mention. (这是local意思。)未定义的宏将被评估为空字符串,并带有您提到的结果。

返回列表<object>变量;<div id="text_translate"><p> 我正在做一个房间数据库</p><pre> @Query("SELECT distinct UserID FROM USER WHERE CovidStat=='T'") public List<USER> checkCovid(); @Query("SELECT DISTINCT PID,VisitDate FROM USER_PLACE,USER WHERE User.UserID=USER_PLACE.UID AND UID=(:UID)") int selectCovidPlace(int UID);</pre><p> 我希望 checkCovid() 返回 UserID,其中 CovidStat='T' 并且我想将 ID 放入 selectCovidPlace 以识别该 ID 去过的地方。 问题是 checkCovid 会返回一个列表而不是 1 个变量,因为它不仅有 1 个人会有 CovidStat='T',但我不确定如何将列表放入 selectCovidPlace()。</p></div></object> - Returning List<Object> to variables;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Stata 中使用 .csv 文件将观察值和变量添加到数据集 - Adding observations and variables to a dataset with .csv files in Stata 帮助使用Uploadify重命名文件 - Help With Renaming Files With Uploadify 使用列表重命名数据集 - Renaming a Dataset using a list 如何在SAS或Stata或R或Excel中打开SPSS元数据文件? - How to open SPSS metadata files in either SAS or Stata or R or Excel? 使用变量创建MySQL数据库 - Creating MySQL database with variables 具有值列表的Django变量 - Django variables with a list of values 返回列表<object>变量;<div id="text_translate"><p> 我正在做一个房间数据库</p><pre> @Query("SELECT distinct UserID FROM USER WHERE CovidStat=='T'") public List<USER> checkCovid(); @Query("SELECT DISTINCT PID,VisitDate FROM USER_PLACE,USER WHERE User.UserID=USER_PLACE.UID AND UID=(:UID)") int selectCovidPlace(int UID);</pre><p> 我希望 checkCovid() 返回 UserID,其中 CovidStat='T' 并且我想将 ID 放入 selectCovidPlace 以识别该 ID 去过的地方。 问题是 checkCovid 会返回一个列表而不是 1 个变量,因为它不仅有 1 个人会有 CovidStat='T',但我不确定如何将列表放入 selectCovidPlace()。</p></div></object> - Returning List<Object> to variables; 从文本文件读取变量/将变量写入变量 - reading/writing variables from text files to variables 在MySQL中创建列表 - creating a list in MySQL 在数据库中创建排序列表 - creating a sorted list in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM