楼主: zhufengyuan
3959 21

[原创博文] 求每条记录出现的次数和时间。 [推广有奖]

11
chendonghui1987 发表于 2012-7-11 11:12:09
用transpose是最好的理解起来也最方便,给你程序.
data work.a;
        input num $ date yymmdd10.;
        format date yymmdd10.;
        datalines;
600203 2012-01-01
600203 2012-01-15
600204 2012-01-14
600205 2012-01-04
600205 2012-01-19
600206 2012-01-03
600206 2012-01-28
600207 2012-01-05
600208 2012-01-09
;
run;

proc sort data = work.a;
        by num date;
run;

proc transpose data = work.a out = work.c(drop = _NAME_);
        by num;
        var date;
run;

proc sql noprint;
        create table work.b as
                select num,count(date) as count
                from work.a
                group by num;
quit;

data work.d;
        merge work.c work.b;
        by num;
run;

12
zhufengyuan 发表于 2012-7-11 11:35:48
谢谢,不过用transpose 会把重复的算进去的,怎样踢出重复值呢~~~
每天进步一点点~~~

13
sailingyf 发表于 2012-7-11 12:47:42
zhufengyuan 发表于 2012-7-11 10:42
do result;
set b;
by num;
retain no.  就是保持no 在下一条记录的值与这一条不变。这样能做到每出现一次加一次1

  array date date_1 - date_&n.   这样应该可以, 你试试

14
ziyenano 发表于 2012-7-11 13:07:53
data ex;
input num date:$10.;
cards;
    600203                2012-01-01
    600203                2012-01-15
    600204                2012-01-14
    600205                2012-01-04
    600205                2012-01-19
    600206                2012-01-03
    600206                2012-01-28
    600207                2012-01-05
    600208                2012-01-09
        ;
        run;
data _null_;
length char $1000 ;
if _n_=1 then do;
declare hash h();
h.definekey('num');
h.definedata('num','char','cishu');
h.definedone();
end;
set ex;
if h.find() ne 0 then do;
char=date;
cishu=1;
h.add();
end;
else do;
char=compress(char||','||date);
cishu=cishu+1;
h.replace();
end;
h.output(dataset:'ex1');
run;
proc sql noprint;
select max(cishu) into:count from ex1;
quit;
%macro fenge;
%do nn=1 %to &count;
date&nn=scan(char,&nn,',');
%end;
%mend;
data ex2;
set ex1;
length  date1-%sysfunc(compress(date&count)) $10;
%fenge;
run;

15
zhufengyuan 发表于 2012-7-11 16:57:17
程序已解决,多谢各位大牛~~
每天进步一点点~~~

16
changweijiadao 发表于 2012-7-12 10:29:27
data a;
input number $ date yymmdd10. @@;
format date yymmdd10.;
cards;
600203 2012-01-01
600203 2012-01-15
600204 2012-01-14
600205 2012-01-04
600205 2012-01-19
600206 2012-01-03
600206 2012-01-28
600207 2012-01-05
600208 2012-01-09
;
run;

proc sort data=a;
by number;
run;

data b;
set a;
by number;
if first.number=1 then cishu=1;
else cishu+1;
run;

data c(rename=(date=date1)) d(rename=(date=date2));
set b;
if cishu=1 then output c;
if cishu=2 then output d;
run;

data e;
retain number date1 date2 cishu;
merge c d;
by number;
run;




data a;
input number $ date yymmdd10. @@;
format date yymmdd10.;
cards;
600203 2012-01-01
600203 2012-01-15
600204 2012-01-14
600205 2012-01-04
600205 2012-01-19
600206 2012-01-03
600206 2012-01-28
600207 2012-01-05
600208 2012-01-09
;
run;

proc sort data=a;
by number;
run;

proc transpose data=a out=b(drop=_NAME_  rename=(COL1=date1 COL2=date2));
by number;
var date;
run;

data c;
set b;
if date1 ne . and   date2 ne .  then cishu=2;
else cishu=1;
run;

17
YueweiLiu 发表于 2012-7-13 22:09:14
呵呵,来晚了。用下面的方法,可以更general一些,随便你有多少date
  1. data have;
  2. input number $ date :yymmdd10.;
  3. format date yymmddd10.;
  4. cards;
  5.     600203                2012-01-01
  6.     600203                2012-01-15
  7.     600204                2012-01-14
  8.     600205                2012-01-04
  9.     600205                2012-01-19
  10.     600206                2012-01-03
  11.     600206                2012-01-28
  12.     600207                2012-01-05
  13.     600208                2012-01-09
  14. ;


  15. proc transpose data=have out=tmp(drop=_name_) prefix=date;
  16.         var date;
  17.         by number;
  18. run;

  19. data want;
  20.         set tmp;
  21.         cishu=n(of date:);
  22. run;
复制代码

18
zhufengyuan 发表于 2012-7-16 10:59:51
YueweiLiu 发表于 2012-7-13 22:09
呵呵,来晚了。用下面的方法,可以更general一些,随便你有多少date
已解决,多谢~~~
每天进步一点点~~~

19
yunqingwang 在职认证  发表于 2012-7-16 11:09:04
data a;
input number $                date:yymmdd10. ;
cards;
600203                2012-01-01
    600203                2012-01-15
    600204                2012-01-14
    600205                2012-01-04
    600205                2012-01-19
    600206                2012-01-03
    600206                2012-01-28
    600207                2012-01-05
    600208                2012-01-09
;
run;
  
proc transpose data=a out=ta(drop=_name_) prefix=date;  
var date;
by number;
run;
data b;
set ta;
cishu=n(of date:);
run;

20
yunqingwang 在职认证  发表于 2012-7-16 11:09:54
sailingyf 发表于 2012-7-11 00:36
只会有date1 和date2 ?
还是出现一次就算一次,会有date3  date4  date5...?
如果是前者的话,很简单...如 ...
data a;
input number $                date:yymmdd10. ;
cards;
600203                2012-01-01
    600203                2012-01-15
    600204                2012-01-14
    600205                2012-01-04
    600205                2012-01-19
    600206                2012-01-03
    600206                2012-01-28
    600207                2012-01-05
    600208                2012-01-09
;
run;
  
proc transpose data=a out=ta(drop=_name_) prefix=date;  var date; by number; run; data b; set ta; cishu=n(of date:); run;

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 20:31