全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2750 6
2009-12-05
悬赏 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 ...
二维码

扫码加我 拉你入群

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

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

全部回复
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;
二维码

扫码加我 拉你入群

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

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

2009-12-5 18:55:10
复制代码
二维码

扫码加我 拉你入群

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

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

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

扫码加我 拉你入群

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

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

2009-12-7 08:40:53
复制代码
二维码

扫码加我 拉你入群

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

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

2009-12-10 14:43:07
是不是写错了啊  数据集c是不是找相同的 index 下的 x1 的某些值,而不是相同的code 下的
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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