楼主: georgiagh
7531 11

sas对数据集合并的问题 [推广有奖]

  • 0关注
  • 0粉丝

大专生

48%

还不是VIP/贵宾

-

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

楼主
georgiagh 发表于 2009-12-1 13:45:04 |AI写论文
5论坛币
一共两个数据集a 和 b,
a:共4个变量                                                     
      index           code    year        x
      12003           1        2003       1
      12004           1        2004       2
      12005           1        2005       3
      22004           2        2004       4
      42005           4        2005       5
b:共5个变量
     index             code    year       y
     12003             1         2003     6
     12004             1         2004     7
     22005             2         2005     8
     32005             3         2005     9
(1)想要生成一个新的数据集c:(即相同的变量保留,不同的变量合并)
    index              code     year        x           y
   12003               1         2003       1           6
   12004               1         2004       2           7
   12005               1         2005       3           .
   22004               2         2004       4           .
   22005               2         2005       .            8
   42005               4         2005       5           .
   32005               3         2005       .            9
(2)生成一个新的数据集d:(要求,提取相同code各year都有的数据)
  index                code      year       x         y
  12003                1          2003      1         6
  12004                1          2004      2         7
  12005                1          2005      3         .

等待高手的解答~多谢~

关键词:数据集 Index year code 变量合并 数据 SAS

回帖推荐

lvjinghui 发表于12楼  查看完整内容

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 ...

yzzhang 发表于10楼  查看完整内容

两个问题的解答,试了下符合楼主要求,前面回帖,太轻率了,没仔细看 *问题1; 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; *问题2; proc sort data=c out=c_temp nodupkey; by code year; run; data d_code(keep=code); set c_temp; by code year; retain n 0; n=n+1; if last.code and n ...

本帖被以下文库推荐

好好学习,天天向上

沙发
gooddrug 发表于 2009-12-1 21:33:38
proc sql;
create table d as
  select *
  from a,b
  where a.code=b.code
   and a.year=b.year;
可以解决(2)生成一个新的数据集d:(要求,提取相同code各year都有的数据)的问题

藤椅
luohuaping 发表于 2009-12-1 21:36:01
按某个变量排序(最好是序号),然后用merge语句合并两个数据得到c,用按code变量排序得到d。
data a;
input index  code    year     x        ;
cards;
……
;
proc sort;
by index;
run;
data b;
input index             code    year       y ;
cards;
……
;
proc sort;
by index;
run;
data c;
merge a b;
by index;
data d;
set c;
by code;

板凳
gooddrug 发表于 2009-12-1 21:48:01
我已经想到怎么解决(1)想要生成一个新的数据集c:(即相同的变量保留,不同的变量合并)的问题了
proc sql;
create table c as
  select *
  from b left join a
  on b.code=a.code
   and b.year=a.year;

报纸
lcwiss 发表于 2009-12-1 21:54:37
data a;
input   index           code    year        x;
datalines;
  12003           1        2003       1
      12004           1        2004       2
      12005           1        2005       3
      22004           2        2004       4
      42005           4        2005       5

;
run;
data b;
input index code year y;
datalines;
12003             1         2003     6
     12004             1         2004     7
     22005             2         2005     8
     32005             3         2005     9

;
data c;
merge a b;
by index code;
run;
proc print data=c;
run;
proc sql;
select a.index,a.year,a.code,x,y
from a,b
where a.year=b.year and a.code=b.code;
新手初次尝试,楼主试一下吧,鉴于你给定的数据我没有对数据a,b集用proc sort 分类,对于一般的数据集最好先分类,哈哈

地板
pyrsh 发表于 2009-12-2 14:10:38
回答问题2,楼主是想要年份齐全的纪录吧??就是03,04,05年都存在的纪录。比较有意思的问题。
接替5楼Data步C的程序,我就不复制了
请指正,需要的话我再解释。

proc sort data=c out=c0;
        by code year;
run;
data c1;
        set c0;
        year_lag = lag(year);
run;

data c2;
        retain conseq year_first;
        set c1;
        by code year;

        if first.code then do;
                year_first=year;
                conseq=0;
        end;
        else do;
                if year ^= year_lag +1 then conseq=1;
                if last.code then do;
                        *change the year value and year_first value to use other criterias;
                        if year = 2005 and year_first = 2003 and conseq =0 then output;
                end;
        end;
run;

data d;
        merge c2(keep = code IN=A) c0;
        by code;
        IF A;
run;

Data步C1最好不要与C2合并,lag这个公式比较娇贵,最好独立Data步

7
yzzhang 发表于 2009-12-2 15:54:24
问题2,直接按code进行merge,对b数据集进行where 限定
data d;
merge a b(where=(year^=.));
by code;
…………

8
niumou 发表于 2009-12-3 11:21:39
回答第一问:
data dta;
        input index code year x;
        datalines;
        12003           1        2003       1
        12004           1        2004       2
         12005           1        2005       3
        22004           2        2004       4
        42005           4        2005       5
      ;
run;
data dtb;
        input index code year y;
        datalines;
        12003             1         2003     6
        12004             1         2004     7
        22005             2         2005     8
        32005             3         2005     9
  ;
run;

proc sort data = dta;
by index code year;
run;

proc sort data = dtb;
by index code year;
run;

data result;
        merge
          dta (in=a)
          dtb (in=b)
          ;
        by index code year;
        imatch = compress(a!!b);
run;

输出结果:
result                                                                                                                                                                                    11:14 Friday, December 3, 2009   3

Obs    index    code    year    x    y    imatch

1     12003      1     2003    1    6      11  
2     12004      1     2004    2    7      11  
3     12005      1     2005    3    .      10  
4     22004      2     2004    4    .      10  
5     22005      2     2005    .    8      01  
6     32005      3     2005    .    9      01  
7     42005      4     2005    5    .      10  
马上送上第二问

9
niumou 发表于 2009-12-3 11:22:25
第二问不会了,我想简单了,sorry

10
yzzhang 发表于 2009-12-3 15:52:13
两个问题的解答,试了下符合楼主要求,前面回帖,太轻率了,没仔细看
*问题1;
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;
*问题2;
proc sort data=c out=c_temp nodupkey; by code year; run;
data d_code(keep=code); set c_temp; by code year;
        retain n 0; n=n+1;
        if last.code and n=2005-2003+1 then output;
run;
data d;
        merge d_code(in=a) c; by code;
        if a;
run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

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

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