楼主: kongqunyu
3976 3

[问答] 不同长度变量数据合并 [推广有奖]

  • 10关注
  • 2粉丝

已卖:91份资源

副教授

37%

还不是VIP/贵宾

-

威望
0
论坛币
2977 个
通用积分
0.4469
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
14732 点
帖子
182
精华
0
在线时间
1274 小时
注册时间
2008-7-16
最后登录
2019-1-7

楼主
kongqunyu 发表于 2011-12-8 15:51:38 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
这两个数据是SAS文件数据, 本是来自同一个数据库 ,长度不同  希望合并的时候每个变量是最大长度。

请提供程序  因为还有很多这样的数据库  要连在一起  
二维码

扫码加我 拉你入群

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

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

关键词:数据合并 数据库 在一起 程序 数据库

mmhc.rar
下载链接: https://bbs.pinggu.org/a-1010558.html

176.86 KB

本附件包括:

  • sq2.sas7bdat
  • sq4.sas7bdat

沙发
hopewell 发表于 2011-12-9 09:14:40
  1. %macro test(indslist=,outds=);
  2.     %local dslist1 dslist2 nomatchvar;
  3.     data _cntlin;
  4.     length start $50;
  5.     retain fmtname 'dsid' type 'i';
  6.     %let num=1;
  7.     %let dsname=%scan(&indslist,&num,%str( ));
  8.     %do %while(&dsname ne);
  9.         %if %sysfunc(exist(&dsname))=0 %then %put ERROR-&dsname does not exist.;
  10.         %if %index(&dsname,%str(.))=0 %then %let dsname=work.&dsname;
  11.         %let dslist1=&dslist1 %upcase("&dsname");
  12.         %let dslist2=&dslist2 &dsname;
  13.         start=%upcase("&dsname");
  14.         label=#
  15.         output;
  16.         %let num=%eval(&num+1);
  17.         %let dsname=%scan(&indslist,&num,%str( ));
  18.     %end;
  19.     run;
  20.     proc format cntlin=_cntlin; run;
  21.     proc sql noprint;
  22.         drop table _cntlin;
  23.         create table __temp as
  24.             select upcase(name) as name, type, length,
  25.                    input(catx('.',input(catx('.',libname,memname),dsid.),varnum),best.) as varid
  26.                 from dictionary.columns
  27.                 where catx('.',libname,memname) in(&dslist1)
  28.                 order by name;
  29.         select a.name into :nomatchvar separated by ','
  30.             from __temp(where=(type='char')) as a, __temp(where=(type='num')) as b
  31.                 where a.name=b.name;
  32.     quit;
  33.     %if &nomatchvar ne %then %do;
  34.         %put ERROR-***Variable &nomatchvar has been defined as both character and numeric.***;
  35.         %goto exit;
  36.     %end;
  37.     data __temp;
  38.         maxlength=0; minvarnum=999;
  39.         do _n_=1 by 1 until(last.name);
  40.             set __temp;
  41.             by name;
  42.             maxlength=max(maxlength,length);
  43.             minvarnum=min(minvarnum,varid);
  44.         end;
  45.         keep name type maxlength minvarnum;
  46.     run;
  47.     proc sort data=__temp out=__temp;
  48.         by minvarnum;
  49.     run;
  50.     data &outds;
  51.     %let dsid=%sysfunc(open(__temp,i));
  52.     %let num=%sysfunc(attrn(&dsid,nlobs));
  53.     %if &num gt 0 %then %do;
  54.         %syscall set(dsid);
  55.         length
  56.         %do i=1 %to #
  57.             %let rc=%sysfunc(fetchobs(&dsid,&i));
  58.             &name %if &type=char %then %str($); &maxlength
  59.         %end;;
  60.     %end;
  61.     %let rc=%sysfunc(close(&dsid));
  62.     set &dslist2;
  63.     format _all_;
  64.     informat _all_;
  65.     run;
  66.     proc delete data=__temp; run;
  67.     %exit:
  68.     proc catalog catalog=work.formats;
  69.         delete dsid.infmt;
  70.     quit;
  71. %mend test;

  72. data a;
  73.     input x $ y;
  74. datalines;
  75. aaa 1
  76. bbb 2
  77. ;
  78. data b;
  79.     length x $20;
  80.     input x y z $;
  81. datalines;
  82. aaaaa 1 aa
  83. bbb 2 b
  84. ;
  85. %test(indslist=a b,outds=z)
复制代码
观钓颇逾垂钓趣 种花何问看花谁

藤椅
kongqunyu 发表于 2011-12-9 09:27:14
hopewell 发表于 2011-12-9 09:14
谢谢  我看不懂 先用了

板凳
kongqunyu 发表于 2011-12-9 09:27:42
hopewell 发表于 2011-12-9 09:14
谢谢  我看不懂 先用了

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

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