全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1948 5
2012-09-25
目前有一个总数据集,其中有一个字段是城市,大概有20个左右不同的城市,另外还有此字段为空的情况。
现在我想按照不同的城市分别生成小的数据集,里面保留城市字段。
请问高手有没有简单的方法?

多谢了!!!
二维码

扫码加我 拉你入群

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

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

全部回复
2012-9-25 19:19:52
就是根据一个变量中的所有不同值生成子集。请问用宏该怎么写呢?
二维码

扫码加我 拉你入群

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

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

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);



二维码

扫码加我 拉你入群

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

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

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 ...
谢谢你的方案,不过还没有看懂,正在学习中。。。。
二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

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;
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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