如何标记扩展宏(本地 :dir )? [英] How to tokenize a extended macro (local :dir )?

查看:46
本文介绍了如何标记扩展宏(本地 :dir )?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的标题令人困惑,因为 tokenize 命令被指定为一个字符串.

I know my title is confusing in the sense that the tokenize command is specified to a string.

我有很多文件夹,里面有大量的、分离的、名字不正确的 Excel 文件(其中大部分是从 ahe 网站上抓取的).手动选择它们很不方便,所以我需要依靠Stata扩展宏函数local :dir来读取它们.

I have many folders that contain massive, separated, ill-named Excel files (most of them are scraped from ahe website). It's inconvenient to select them manually so I need to rely on Stata extended macro function local :dir to read them.

我的代码如下:

foreach file of local filelist {
    import excel "`file'", clear
    sxpose, clear 
    save "`file'.dta", replace
}

这样的代码会生成许多新的dta 文件,因此目录中充满了这些文件.我更喜欢为第一个 xlsx 文件创建一个新的数据文件,然后在 foreach 循环内将其他文件append 附加到它.所以本质上,循环中有一个 if-else .

Such code will generate many new dta files and the directory is thus full of these files. I prefer to create a single new data file for the first xlsx file and then append others to it inside the foreach loop. So essentially, there's a if-else inside the loop.

我们需要一个刚刚创建的宏 filelist 的索引,以便我们可以编写如下内容:

We need an index of the macro filelist just created, so that we can write something like:

token `filelist'  // filelist is created in the former code

if "`i'" == `1' {
   import excel "`file'",clear
}
else {
   append using `i',clear
}

我知道我的代码效率低下且容易出错:表达式 token 'filelist' 的语法也不正确(假设 filelist 不是字符串).但是,我仍然想弄清楚我的伪代码背后的基本结构.

I know my code is inefficient and error-prone: the syntax of expression token 'filelist' is incorrect too (given that filelist is not a string). However, I still want to figure out the basic structure behind my pseudo code.

我怎样才能更正我的代码并使其工作?

How could I correct my code and make it work?

非常欢迎另一种更有效的方法.

Another more efficient approach is highly welcomed.

推荐答案

脑海中浮现出各种技术,但没有一个需要标记化.

Various techniques spring to mind, none of which entails tokenizing.

local count = 1 
foreach file of local filelist {
    import excel "`file'",clear
    sxpose, clear 

    if `count' == 1 save alldata 
    else append using alldata 

    local ++count
}


local allothers "*" 
foreach file of local filelist {
    import excel "`file'",clear
    sxpose, clear 

    `firstonly'   save alldata 
    `allothers'   append using alldata 

    local firstonly "*" 
    local allothers 
}

在第二个块中,重点是以 * 为前缀的行被视为注释,因此 * 前面的任何命令都将被忽略(注释掉").append 语句在循环中第一次被注释掉,并且 save 语句前面是一个未定义的本地宏,它的计算结果为一个空字符串,所以它是 不被忽视.

In the second block, the point is that lines prefixed by * are treated as comments, so any command that * precedes is ignored ("commented out"). The append statement is commented out first time round the loop and the save statement is preceded by an undefined local macro, which evaluates to an empty string, so it is not ignored.

在第一次循环之后,对 append 的注释被删除,但放在 save 上.

After the first time round the loop, commenting out on append is removed, but placed on the save.

我不认为这两种方法中的任何一种都比您想象的更有效(工作速度更快、使用更少的内存、更短,或者任何对您来说有效"意味着什么).代码显然确实以您已正确设置文件列表为前提.

I don't think either of these approaches is more efficient than what you have in mind (works faster, uses less memory, is shorter, or whatever "efficient" means for you). The code clearly does presuppose that you have set up the file list correctly.

这篇关于如何标记扩展宏(本地 :dir )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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