全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
1152 1
2016-02-27

I have thousands of city folders (say city1, city2, and so on, but in reality named like 北京, 上海, etc.)

Each folder further contains two subfolders: land and house.

So the directory structure is like


currentdictionary

  ---- city1

     ----- house

         ------ many .xlsx files

     ----- land

  ----- city2

  ----- city3

  ···


  ----- city1000


I want to get the complete list of all subdirs and do some manipulation (like import excel). I know there is a macro extended function:

复制代码


to handle this issue, but it seems it can only return the first tier of subdirs, like city_i, rather than those deeper ones.

I use the following snippet of codes to test my ideas, if the approach works, it should return a full list of city names and each city's project


set more off

cdG:\Data_backup\Soufang_data

local folder:dir . dirs "*"

foreach i oflocal folder {

     di "`i'"

     local `i'_house : dir "G:\Data_backup\Soufang_data/`i'\house" files"*.xlsx"

     foreach j of local `i'_house {

        di "`j'"

     }


}



However, the outcome on the screen is something like

city1

project100

project99

······


project1



It seems the code only loops one round, over the first city, but fails to come to city2,city3 and so on. I suspect it's due to my problematic writing of the local,espcially in this line

复制代码

but I'm not sure.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-3-1 11:10:21
I think the most efficient way is to directly define a local macro containing all the city names, which you can get from running the dir comamnd or ls command. Copy the output of the command, paste into Excel, and keep only the city names. I do this kind of thing all the time. It takes only a minute to do.

Because this is so quick and easy, I never bothered to find a Stata command for it.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群