楼主: yumenderen
6147 21

[问答] 如何合成多个数据集为1个数据集 [推广有奖]

  • 1关注
  • 2粉丝

讲师

7%

还不是VIP/贵宾

-

威望
0
论坛币
150 个
通用积分
0
学术水平
2 点
热心指数
2 点
信用等级
2 点
经验
6843 点
帖子
291
精华
0
在线时间
475 小时
注册时间
2007-12-28
最后登录
2021-5-23

楼主
yumenderen 发表于 2011-9-8 14:41:16 |AI写论文
10论坛币
请教达人:
       已经有多个数据结构一样的数据集,并且数据集名称都是“com”开头的,比如数据集名称分别为comzhangsan,comlisi,comzhaowu;想把这3个数据集合并成一个数据集并在各个数据集后面增加一列和数据集名称一样的字段以便区分,如comzhangsan里面增加一列type都等于"comzhangsan",comlisi里增加一列type都等于“comlisi”等,
       由于数据集较多,想弄个程序实现一下,该怎么写呢?如果不是com开头没有规律又该咋写呢?谢谢大家啦

最佳答案

yzzhang 查看完整内容

libname a 'd:\'; data a.m; x=1; run; data a.n; x=1; run; %macro ds_union(libnm); proc sql; create table dict_dsnm(where=(libnm=upcase("&libnm."))) as select distinct memname as dsnm, libname as libnm from dictionary.members where memtype='DATA'; select count(distinct dsnm) into :ds_n from dict_dsnm; quit; %do i=1 %to &ds_n.; data dsnm; set dict_dsnm; ...
关键词:数据集 Hang type 数据结构 请教达人 如何 程序

本帖被以下文库推荐

沙发
yzzhang 发表于 2011-9-8 14:41:17
libname a 'd:\';
data a.m;
        x=1;
        run;
data a.n;
        x=1;
        run;


%macro ds_union(libnm);
proc sql;
        create table dict_dsnm(where=(libnm=upcase("&libnm."))) as select distinct
                memname as dsnm, libname as libnm
        from dictionary.members        where memtype='DATA';
        select count(distinct dsnm) into :ds_n from dict_dsnm;
        quit;
%do i=1 %to &ds_n.;
    data dsnm;
        set dict_dsnm;
        if _N_=&i.;
        call symput('dsnm',dsnm);
        run;
    data ds;
        set &libnm..&dsnm;
        length type $50;
        type='&dsnm.';
        run;

    data dsnm;
        set &libnm..&dsnm.;
        length type $50;
        type="&dsnm.";
        run;
        proc append base=ds_union data=dsnm force; quit;
%end;
%mend ds_union;

%ds_union(a);

藤椅
yugao1986 发表于 2011-9-8 15:11:11
试一试set,利用指示变量新建type变量
三人行必有我师

板凳
yumenderen 发表于 2011-9-8 15:15:51
楼上的能说的具体点么?俺是菜鸟弄不懂啊

报纸
priss111 发表于 2011-9-8 15:32:09
试一下用这个宏参数,
看能不能实现
  1. %let n=3;
  2. %macro data(new=, old=);

  3. data &new;
  4. set &old;
  5. group=&old;
  6. run;

  7. data all;
  8. set data1-data&n;
  9. run;
  10. %mend data;

  11. %data(new=data1, old=comzhangsan)
  12. %data(new=data2, old=comlisi)
  13. %data(new=data3,old=comzhaowu)
复制代码

地板
yumenderen 发表于 2011-9-8 15:44:50
priss111 发表于 2011-9-8 15:32
试一下用这个宏参数,
看能不能实现
谢谢你,提供了一个较好地解决方案
只是如果不是3个数据集,而是10个的话,感觉就要执行10个宏。
就没有什么函数或者什么方法的获取数据集名称么

7
priss111 发表于 2011-9-8 16:10:40
yumenderen 发表于 2011-9-8 15:44
谢谢你,提供了一个较好地解决方案
只是如果不是3个数据集,而是10个的话,感觉就要执行10个宏。
就没有 ...
这个我就不会了
得请高手们指教了

8
yumenderen 发表于 2011-9-8 16:13:13
priss111 发表于 2011-9-8 16:10
这个我就不会了
得请高手们指教了
呵呵,非常感谢你的帮助

9
yzzhang 发表于 2011-9-8 16:57:52
假设逻辑库a下有n个数据集;
%macro ds_union(libnm);
proc sql;
    create table dict_dsnm as select distinct
         memname as dsnm
    from dictionary.members
    where memtype='DATA' and libname="&libnm.";
    select count(distinct dsnm) into :ds_n from dict_dsnm;
    quit;
%do i=1 %to &ds_n.;
    data dsnm;
        set dict_dsnm;
        if _N_=&i.;
        call symput('dsnm',dsnm);
        run;
    data ds;
        set &dsnm;
        length type $50;
        type='&dsnm.';
        run;
    %if &i.=1 %then %do;
    data ds_union;
        set &dsnm.;
        length type $50;
        type="&dsnm.";
        run;
    %end;
    %else %do;
    proc sql;
        create table ds_union as
        select "&dsnm." as type, * from ds_union
        union       
        select "&dsnm." as type, * from &dsnm;
        quit;
    %end;
%end;
%mend ds_union;

%ds_union(a);



10
yumenderen 发表于 2011-9-8 17:25:10
yzzhang 发表于 2011-9-8 16:57
假设逻辑库a下有n个数据集;
%macro ds_union(libnm);
proc sql;
达人,好像搞不定呀

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 05:54