楼主: lovelybetty1208
1712 5

[问答] 如何拆分生成若干个数据集 [推广有奖]

  • 0关注
  • 1粉丝

博士生

48%

还不是VIP/贵宾

-

威望
0
论坛币
97 个
通用积分
0
学术水平
1 点
热心指数
5 点
信用等级
1 点
经验
17473 点
帖子
294
精华
0
在线时间
147 小时
注册时间
2011-11-5
最后登录
2015-7-11

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
目前有一个总数据集,其中有一个字段是城市,大概有20个左右不同的城市,另外还有此字段为空的情况。
现在我想按照不同的城市分别生成小的数据集,里面保留城市字段。
请问高手有没有简单的方法?

多谢了!!!
二维码

扫码加我 拉你入群

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

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

关键词:数据集 有没有 如何

耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。
沙发
lovelybetty1208 发表于 2012-9-25 19:19:52 |只看作者 |坛友微信交流群
就是根据一个变量中的所有不同值生成子集。请问用宏该怎么写呢?
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

使用道具

藤椅
bobguy 发表于 2012-9-26 09:25:13 |只看作者 |坛友微信交流群
It needs two passes to have it done. First one needs get unique city name list. Second, based upon the result proper sas program needs to be  generated. If a city name will be a data set name, it must be concur with SAS name convention.



data city;
  length city $20;
  do city='shanghai','bejing','nanjin','tianjing';
    n=ceil(ranuni(123)*10);
        do i=1 to n;
        othervar=rannor(123);
        output;
        end;
  end;
run;



%macro split(dsn_in=);
proc sql noprint;
  select distinct city into: city1 - : city3000
  from city;
  %let n=&sqlobs;
  quit;


  data %do i=1 %to &n;
         &&city&i
           %end;
           ;
           set &dsn_in;
       if city="&city1" then output &city1;
           %do i=2 %to &n-1;
             else if city="&&city&i" then output &&city&i;
           %end;
       else output &&city&n;
  run;
%mend;
options mprint;
%split(dsn_in=city);



使用道具

板凳
lovelybetty1208 发表于 2012-9-26 12:22:42 |只看作者 |坛友微信交流群
bobguy 发表于 2012-9-26 09:25
It needs two passes to have it done. First one needs get unique city name list. Second, based upon t ...
谢谢你的方案,不过还没有看懂,正在学习中。。。。
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

使用道具

报纸
bobguy 发表于 2012-9-27 07:50:10 |只看作者 |坛友微信交流群
lovelybetty1208 发表于 2012-9-26 12:22
谢谢你的方案,不过还没有看懂,正在学习中。。。。
tip:   Don't learn SAS macro until you have solid data step programming skill.

使用道具

地板
pobel 在职认证  发表于 2012-9-27 09:28:13 |只看作者 |坛友微信交流群
data city;
  length city $20;
  do city='shanghai','bejing','nanjin','tianjing';
    n=ceil(ranuni(123)*10);
        do i=1 to n;
        othervar=rannor(123);
        output;
        end;
  end;
run;

proc sql noprint;
   select distinct strip("_"||city) into : citylist separated by " "
    from city;
quit;

%put &citylist;


data _null_;
    length code $1000;
        retain code;
    set city end=last;
        by city notsorted;
        if _n_=1 then code="data &citylist; set city;";
        if first.city then do;
           count+1;
       if count=1 then code=strip(code)||"if city="||quote(strip(city))||" then output _"||strip(city)||";";
           else if count>1 then code=strip(code)||"else if city="||quote(strip(city))||" then output _"||strip(city)||";";
        end;
    if last then do;
         code=strip(code)||" run;";
                put code=;
                call execute(code);
        end;

run;
和谐拯救危机

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

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

GMT+8, 2024-4-28 15:41