楼主: zlqs1985
1058 1

[编程问题求助] 如何在local中正确嵌套local (附一个应用) ? [推广有奖]

  • 2关注
  • 17粉丝

副教授

2%

还不是VIP/贵宾

-

威望
0
论坛币
1020 个
通用积分
12.0359
学术水平
2 点
热心指数
6 点
信用等级
2 点
经验
8733 点
帖子
324
精华
0
在线时间
815 小时
注册时间
2008-9-18
最后登录
2023-1-16

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

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:

  1. local list: dir
复制代码


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

  1. foreach j of local `i'_house
复制代码

but I'm not sure.



二维码

扫码加我 拉你入群

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

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

关键词:Local OCA manipulation problematic Dictionary dictionary thousands structure function further

沙发
夏目贵志 发表于 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.

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-11-10 06:03