楼主: georgiagh
2791 6

SAS数据处理 [推广有奖]

  • 0关注
  • 0粉丝

大专生

48%

还不是VIP/贵宾

-

威望
0
论坛币
386 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
445 点
帖子
39
精华
0
在线时间
63 小时
注册时间
2009-11-4
最后登录
2018-9-22

楼主
georgiagh 发表于 2009-12-5 12:51:31 |AI写论文
10论坛币
现有两个数据集a和b
数据集a:
    index           code       year        x1        x2       x3
   102003         10         2003        1           2         3               
   102003         10         2003        2           2         3      
   102004         10         2004        1           4         5
   102005         10         2005        2           4         5
   102006         10         2006        1           6         7
   102006         10         2006        2           7         8
   122003         12         2003        1           8         9
   122004         12         2004        2           9        10
   122005         12         2005        1          10       11
   122005         12         2005        2          10       11
   122006         12         2006        1          11       12
   142004         14         2004        1          12       13
   142005         14         2005        2          13       14
   162005         16         2005        1          14       15
   162006         16         2006        2          15       16
数据集b:
   index             code      year       y1     y2     y3
  102003           10         2003       1       2       3
  102004           10         2004       2       3       4
  102005           10         2005       3       4       5
  102006           10         2006       4       5       6
  122003           12         2003       5       6       7
  122004           12         2004       6       7       8
  122005           12         2005       7       8       9
  122006           12         2006       8       9       10
  152004           15         2004       1       2       3
  162004           16         2004       2       3       4
  172005           17         2005       3       4       5
现欲生成两个数据集c和d
(1)c:即 将首先对a数据集进行处理,对相同的code值,如果x1的值有1,有2,则取x1=1的那条值,把x1=2的那条删去,x1只有1值或只有2值的保留,然后与数据集b进行合并,得如下数据集:
   index          code       year       x1       x2        x3        y1        y2         y3
  102003         10         2003       1         2          3         1          2           3
  102004         10         2004       1         4          5         2          3           4  
  102005         10         2005       2         5          6         3          4           5
  102006         10         2006       1         6          7         4          5           6
  122003         12         2003       1         8          9         5          6           7
  122004         12         2004       2         9         10        6          7           8
  122005         12         2005       1        10        11        7          8           9
  122006         12         2006       1        11        12        8          9          10
  142004         14         2004       1        12        13
  142005         14         2005       2        13        14
  152004         15         2004                                        1            2           3
  162004         16         2004                                        2            3           4
  162005         16         2005       1        14        15
  162006         16         2006       2        15        16
  172005         17         2005                                         3           4           5         

(2)生成数据集d :即提取数据集c中,对code值,在2003-2006年均有的,剔除某一年没有值的比如code为14,15不全的数据;
  index           code      year       x1         x2         x3         y1        y2         y3      
102003         10         2003       1          2            3           1         2           3
102004         10         2004       1          4            5           2         3           4
102005         10         2005       2          5            6           3         4           5
102006         10         2006       1          6            7           4         5           6
122003         12         2003       1          8            9           5         6           7
122004         12         2004       2          9           10          6         7           8
122005         12         2005       1         10          11          7         8           9
122006         12         2006       1         11          12          8         9          10


多谢多谢~~~

最佳答案

sushe1527 查看完整内容

data a; input index code year x1 x2 x3; cards; 102003 10 2003 1 2 3 102003 10 2003 2 2 3 102004 10 2004 1 4 5 102005 10 2005 2 4 5 102006 10 ...
关键词:sas数据处理 数据处理 Index code year 数据处理 SAS 数据分析专题 数据处理 数据分析软件 数据分析报告 面板数据分析 excel数据分析 数据分析方法 项目数据分析
好好学习,天天向上

沙发
sushe1527 发表于 2009-12-5 12:51:32
data a;
input index           code       year        x1        x2       x3;
cards;
   102003         10         2003        1           2         3               
   102003         10         2003        2           2         3      
   102004         10         2004        1           4         5
   102005         10         2005        2           4         5
   102006         10         2006        1           6         7
   102006         10         2006        2           7         8
   122003         12         2003        1           8         9
   122004         12         2004        2           9        10
   122005         12         2005        1          10       11
   122005         12         2005        2          10       11
   122006         12         2006        1          11       12
   142004         14         2004        1          12       13
   142005         14         2005        2          13       14
   162005         16         2005        1          14       15
   162006         16         2006        2          15       16
   ;run;
   data b;
   input index             code      year       y1     y2     y3;
   cards;
  102003           10         2003       1       2       3
  102004           10         2004       2       3       4
  102005           10         2005       3       4       5
  102006           10         2006       4       5       6
  122003           12         2003       5       6       7
  122004           12         2004       6       7       8
  122005           12         2005       7       8       9
  122006           12         2006       8       9       10
  152004           15         2004       1       2       3
  162004           16         2004       2       3       4
  172005           17         2005       3       4       5
;run;proc sort data=b;by code year;run;
data new;set a;
by code year;
if first.code then n=0;
if first.year then n=0;
n+1;if n=1 then output;
drop n;run;
data c;merge new b;by code year;run;
proc sql;create table d as
  select * from new as cross join b
  on new.code=b.code and new.year=b.year; quit;

藤椅
lwien007 发表于 2009-12-5 18:55:10
  1. data tmp;
  2.         set a;
  3.         by index;
  4.         if first.index and last.index then output;
  5.         else if x1=1 then output;
  6. run;
  7. data c ;
  8.         merge tmp b;
  9.         by index;
  10. run;
  11. data d;
  12.         set c;
  13.         by code;
  14.         if first.code then sum=0;
  15.         sum+1;
  16.         if last.code then do;
  17.                 if sum=4 then do i=1 to 4;
  18.                         set c ;
  19.                         output;
  20.                 end;
  21.                 if sum<4 then do i=1 to sum;
  22.                         set c;
  23.                 end;
  24.         end;
  25. run;
复制代码

板凳
yongyitian 发表于 2009-12-6 10:55:00
proc sql;
   create table c as
   select coalesce(a.index, b.index) as index,
          coalesce(a.code, b.code) as code,
          coalesce(a.year, b.year) as year,
          x1, x2, x3, y1, y2, y3
   from a full join b
   on a.index = b.index and a.code=b.code and a.year=b.year
   group by a.index, a.code, a.year
   having x1 = min(x1)
   order by index, code, year;
quit;

proc sql;
  create table temp as
  select a.*, b.y1, b.y2, b.y3
  from a, b
  where a.index=b.index and a.code=b.code and a.year=b.year;
quit;

proc sql;
  create table d as
  select index, code, year, x1, x2, x3, y1, y2, y3
  from temp
  group by index, code, year
  having x1 = min(x1);
quit;

报纸
lwien007 发表于 2009-12-7 08:40:53
  1. data tmp;
  2.         set a;
  3.         by index;
  4.         if first.index and last.index then output;
  5.         else if x1=1 then output;
  6. run;
  7. data c ;
  8.       merge tmp b;
  9.         by index;
  10. run;
  11. data d;
  12. set c;
  13. by code;
  14. if first.code then sum=0;
  15. sum+1;
  16. if last.code then do i=1 to sum;
  17.   set c;
  18.   if sum=4 then output;
  19. end;
  20. run;
复制代码

地板
lvjinghui 发表于 2009-12-10 14:43:07
是不是写错了啊  数据集c是不是找相同的 index 下的 x1 的某些值,而不是相同的code 下的

7
lvjinghui 发表于 2009-12-10 15:03:05
data a;
   infile datalines truncover scanover ;
   input index code year x1 x2 x3 ;
   cards;
   102003         10         2003        1           2         3               
   102003         10         2003        2           2         3      
   102004         10         2004        1           4         5
   102005         10         2005        2           4         5
   102006         10         2006        1           6         7
   102006         10         2006        2           7         8
   122003         12         2003        1           8         9
   122004         12         2004        2           9        10
   122005         12         2005        1          10       11
   122005         12         2005        2          10       11
   122006         12         2006        1          11       12
   142004         14         2004        1          12       13
   142005         14         2005        2          13       14
   162005         16         2005        1          14       15
   162006         16         2006        2          15       16
;
run;
proc sort data=a;
   by index   x1;
run;
data a;
  set a;
      by index   x1;
              if first.index;
run;
data b;
  infile datalines truncover scanover;
  input index code year y1 y2 y3;
  cards;
  102003           10         2003       1       2       3
  102004           10         2004       2       3       4
  102005           10         2005       3       4       5
  102006           10         2006       4       5       6
  122003           12         2003       5       6       7
  122004           12         2004       6       7       8
  122005           12         2005       7       8       9
  122006           12         2006       8       9       10
  152004           15         2004       1       2       3
  162004           16         2004       2       3       4
  172005           17         2005       3       4       5
;
run;
proc sort data=a;
     by index code year;
run;
proc sort data=b;
    by index code year;
run;
data c;
    merge a b;
           by index code year;
run;
proc sql noprint;
    create table d as
           select  c.*,count(code) as count
               from c
                      group by code                             
                          ;
quit;
data d;
   set d;
     where count=4;
           keep index code year x1 x2 x3 y1 y2 y3;
run

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

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