用于更改日期格式和生成图形的循环
Loop for changing date format and generating graphs
我想遍历多个 csv
文件,以更改日期变量的格式并生成图表,每个图表的标题只是相应文件的名称。
这是使用 dataex
创建的 csv
文件之一的示例:
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
这些文件在"I:\Games CSVs"
目录中,它们都是csv
格式。我确保所有文件中的变量名称都相同。
我想获取每个文件,将 scrapedate
从字符串格式转换为日期格式。然后我想绘制由四个变量 average
、thpercentile
、v5
、v6
和 y-axis、ScrapeDate
表示的四条线。
到目前为止,基于 Google 搜索,我尝试了以下代码:
local files : dir "I:\Games CSVs" files "*.csv"
cd "I:\Games CSVs"
foreach file in `files' {
insheet using `file', comma clear
/* Convert string to date format */
gen ScrapeDate = date(scrapedate, "YMD")
format ScrapeDate %td #Convert string to date format
/* X Axis: price, Y Axis: ScrapeDate; Plotting, averages, 25th, 50th and 75th percentile */
line average thpercentile v5 v6 ScrapeDate, legend(size(medsmall))
}
以上代码包含在 do
文件中。问题是当我这样做时,File -> Do.. -> filename.do
,我没有看到任何图表,它只是在我的 window 中显示为 end of do file
。我没有看到图表!
我希望我的代码没有错,因为在左侧面板上,do "path/filename.do"
显示为红色。我已经删除了 do
文件中的所有空行,它没有分号,这与 Google 搜索答案的内容相反(答案有分号)。
我希望能够访问图表!但是它们存储在哪里?
我只用一个文件和下面的代码就能完成我想要的东西:
insheet using "I:\Games CSVs\oneofthecsvfiles.csv"
gen ScrapeDate = date(scrapedate, "YMD")
format ScrapeDate %td
line average thpercentile v5 v6 ScrapeDate, legend(size(medsmall))
最后,上面的代码将日期变量转换为数字,但将格式从原来的 2015-12-17
更改为 17dec2015
。
有没有办法让它成为 2015/12/17
或 2015-12-17
?
假设您有以下玩具数据集:
local foodir // INSERT YOUR DIRECTORY PATH HERE
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
export delimited using "`foodir'one", replace
clear
input str32 eventname str10 scrapedate float(average thpercentile v5 v6)
"EventName" "2014-12-15" 236.9255 83.2 104.875 148.75
"EventName" "2014-12-16" 230.4555 78.55 99 138.22
"EventName" "2014-12-17" 223.66705 72.7 90.25 131.2
"EventName" "2014-12-18" 216.45757 64.855 78.55 119.5
"EventName" "2014-12-19" 208.63446 60.56333 72.7 119.07333
"EventName" "2014-12-20" 194.97125 55.15 69.77 112.48
end
export delimited using "`foodir'two", replace
clear
input str32 eventname str10 scrapedate float(average thpercentile v5 v6)
"EventName" "2013-12-15" 336.9255 83.2 104.875 148.75
"EventName" "2013-12-16" 330.4555 78.55 99 138.22
"EventName" "2013-12-17" 323.66705 72.7 90.25 131.2
"EventName" "2013-12-18" 316.45757 64.855 78.55 119.5
"EventName" "2013-12-19" 308.63446 60.56333 72.7 119.07333
"EventName" "2013-12-20" 294.97125 55.15 69.77 112.48
end
以下对我有用:
local foodir // INSERT YOUR DIRECTORY PATH HERE
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 %tdCCYY-NN-DD
line average thpercentile v5 v6 ScrapeDate, name("graph`i'", replace) title("`file'") ///
legend(size(medsmall))
}
只需在本地宏中插入 完整 路径 foodir
Stata 将完成剩下的工作。
我在您的原始代码中所做的所有更改都以粗体显示。
我想遍历多个 csv
文件,以更改日期变量的格式并生成图表,每个图表的标题只是相应文件的名称。
这是使用 dataex
创建的 csv
文件之一的示例:
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
这些文件在"I:\Games CSVs"
目录中,它们都是csv
格式。我确保所有文件中的变量名称都相同。
我想获取每个文件,将 scrapedate
从字符串格式转换为日期格式。然后我想绘制由四个变量 average
、thpercentile
、v5
、v6
和 y-axis、ScrapeDate
表示的四条线。
到目前为止,基于 Google 搜索,我尝试了以下代码:
local files : dir "I:\Games CSVs" files "*.csv"
cd "I:\Games CSVs"
foreach file in `files' {
insheet using `file', comma clear
/* Convert string to date format */
gen ScrapeDate = date(scrapedate, "YMD")
format ScrapeDate %td #Convert string to date format
/* X Axis: price, Y Axis: ScrapeDate; Plotting, averages, 25th, 50th and 75th percentile */
line average thpercentile v5 v6 ScrapeDate, legend(size(medsmall))
}
以上代码包含在 do
文件中。问题是当我这样做时,File -> Do.. -> filename.do
,我没有看到任何图表,它只是在我的 window 中显示为 end of do file
。我没有看到图表!
我希望我的代码没有错,因为在左侧面板上,do "path/filename.do"
显示为红色。我已经删除了 do
文件中的所有空行,它没有分号,这与 Google 搜索答案的内容相反(答案有分号)。
我希望能够访问图表!但是它们存储在哪里?
我只用一个文件和下面的代码就能完成我想要的东西:
insheet using "I:\Games CSVs\oneofthecsvfiles.csv"
gen ScrapeDate = date(scrapedate, "YMD")
format ScrapeDate %td
line average thpercentile v5 v6 ScrapeDate, legend(size(medsmall))
最后,上面的代码将日期变量转换为数字,但将格式从原来的 2015-12-17
更改为 17dec2015
。
有没有办法让它成为 2015/12/17
或 2015-12-17
?
假设您有以下玩具数据集:
local foodir // INSERT YOUR DIRECTORY PATH HERE 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 export delimited using "`foodir'one", replace clear input str32 eventname str10 scrapedate float(average thpercentile v5 v6) "EventName" "2014-12-15" 236.9255 83.2 104.875 148.75 "EventName" "2014-12-16" 230.4555 78.55 99 138.22 "EventName" "2014-12-17" 223.66705 72.7 90.25 131.2 "EventName" "2014-12-18" 216.45757 64.855 78.55 119.5 "EventName" "2014-12-19" 208.63446 60.56333 72.7 119.07333 "EventName" "2014-12-20" 194.97125 55.15 69.77 112.48 end export delimited using "`foodir'two", replace clear input str32 eventname str10 scrapedate float(average thpercentile v5 v6) "EventName" "2013-12-15" 336.9255 83.2 104.875 148.75 "EventName" "2013-12-16" 330.4555 78.55 99 138.22 "EventName" "2013-12-17" 323.66705 72.7 90.25 131.2 "EventName" "2013-12-18" 316.45757 64.855 78.55 119.5 "EventName" "2013-12-19" 308.63446 60.56333 72.7 119.07333 "EventName" "2013-12-20" 294.97125 55.15 69.77 112.48 end
以下对我有用:
local foodir // INSERT YOUR DIRECTORY PATH HERE 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 %tdCCYY-NN-DD line average thpercentile v5 v6 ScrapeDate, name("graph`i'", replace) title("`file'") /// legend(size(medsmall)) }
只需在本地宏中插入 完整 路径 foodir
Stata 将完成剩下的工作。
我在您的原始代码中所做的所有更改都以粗体显示。