Stata 循环遍历文件夹,为每个文件生成并保存图形

Stata 循环遍历文件夹,为每个文件生成并保存图形,stata,stata-macros,Stata,Stata Macros,下面是我的csv文件之一,该文件是从dataex创建的,用于一个可复制的示例: clear input str32 eventname str10 scrapedate float(average thpercentile v5 v6) "EventName" "2015-12-15" 136.9255 83.2 104.875 148.75 "EventName" "2015-12-16" 130.4555 78.55 99 138.22 "Event

下面是我的
csv
文件之一,该文件是从
dataex
创建的,用于一个可复制的示例:

clear

input str32 eventname str10 scrapedate float(average thpercentile v5 v6)
"EventName" "2015-12-15"  136.9255     83.2 104.875    148.75
"EventName" "2015-12-16"  130.4555    78.55      99    138.22
"EventName" "2015-12-17" 123.66705     72.7   90.25     131.2
"EventName" "2015-12-18" 116.45757   64.855   78.55     119.5
"EventName" "2015-12-19" 108.63446 60.56333    72.7 119.07333
"EventName" "2015-12-20"  94.97125    55.15   69.77    112.48
end
多亏了my的答案,我才能够调整代码,使之在目录
“I:\Games CSVs\”
中循环,并使用以下方法读取每个
csv
文件:

insheet using "`file'", comma clear
然后创建一个新变量,将数据格式更改为所需格式,并生成折线图

这是我的密码:

local foodir "I:\Games CSVs\" 
local files : dir "`foodir'" files "*.csv"
cd "`foodir'"
local i = 0
foreach file of local files {
    local ++i
    insheet using "`file'", comma clear
    generate ScrapeDate = daily(scrapedate, "YMD")
    format ScrapeDate %tdYY-NN-DD 
    line average thpercentile v5 v6 ScrapeDate, name("graph`i'", replace) ///
    scale(*.7) ///
    local filename = substr("`file'", 1, strlen("`file'")-4) ///
    title(filename) ///
    ytitle("Price in US$") ///
    legend(size(small)) 
}
有问题的一行如下:

local filename = substr("`file'", 1, strlen("`file'")-4)` 
title(filename)
我还尝试:

generate filename = substr("`file'", 1, strlen("`file'")-4)` 
title(filename)
我有以下问题:

  • 该文件的标题为
    filename.csv
    ,我希望删除后缀
  • 我也不知道如何将图形保存到磁盘上
  • 所有的图表(我有52个)都在一个接一个地闪烁。如果我能将它们全部保存在一个文件夹(
    I:\Graphs
    )中,
    filename
    filename.csv
    相同,而在这里保存为
    filename.png
    filename.jpeg
    或任何我可以打开的格式,这对我来说是非常理想的

    我已经阅读了文件。我相信
    graph save mygraph
    会替换存在的图形,因为我在目录中循环,每次都会替换图形,因为我没有更改图形的名称。

    在使用它之前,您需要正确定义本地宏
    文件名
    。您还需要使用
    命令中的
    saving()
    nodraw
    选项:

    local foodir "I:\Games CSVs\" local foosavedir "I:\Graphs\" local files : dir "`foodir'" files "*.csv" cd "`foodir'" local i = 0 foreach file of local files { local ++i insheet using "`file'", comma clear generate ScrapeDate = daily(scrapedate, "YMD") format ScrapeDate %tdYY-NN-DD local filename = substr("`file'", 1, strrpos("`file'", ".")-1) line average thpercentile v5 v6 ScrapeDate, name("graph`i'", replace) /// saving("`foosavedir'`filename'.gph", replace) nodraw scale(*.7) title("`filename'") /// ytitle("Price in US$") legend(size(small)) } 在这种情况下,您必须在
    行中指定
    nodraw
    选项

    如果指定了选项
    replace
    ,并且保存/导出文件的名称与现有文件冲突,则将替换图形。如果文件名是唯一的,则不应出现此问题。

    在使用本地宏
    文件名之前,需要正确定义它。您还需要使用
    命令中的
    saving()
    nodraw
    选项:

    local foodir "I:\Games CSVs\" local foosavedir "I:\Graphs\" local files : dir "`foodir'" files "*.csv" cd "`foodir'" local i = 0 foreach file of local files { local ++i insheet using "`file'", comma clear generate ScrapeDate = daily(scrapedate, "YMD") format ScrapeDate %tdYY-NN-DD local filename = substr("`file'", 1, strrpos("`file'", ".")-1) line average thpercentile v5 v6 ScrapeDate, name("graph`i'", replace) /// saving("`foosavedir'`filename'.gph", replace) nodraw scale(*.7) title("`filename'") /// ytitle("Price in US$") legend(size(small)) } 在这种情况下,您必须在
    行中指定
    nodraw
    选项

    如果指定了选项
    replace
    ,并且保存/导出文件的名称与现有文件冲突,则将替换图形。如果文件名是唯一的,则不应出现此问题