Sas 将年份变量分配给ID

Sas 将年份变量分配给ID,sas,sas-studio,Sas,Sas Studio,我使用以下代码为每个ID分配年份2017201820192020: proc sort data=Have; by ID; run; data Have2; set Have(keep=id); by id ; if first.id then do Vintage= 2017 to 2020; output; end; run; proc sort data=have2; by id; run; data have3 ; m

我使用以下代码为每个ID分配年份2017201820192020:

proc sort data=Have;
    by ID;
    run;

data Have2;
    set Have(keep=id);
    by id ;
    if first.id then do Vintage= 2017 to 2020;
    output;
    end;
run;

proc sort data=have2;
by id;
run;

data have3 ;
    merge have2 have;
    by id;
run;
这样,数据集就会如下所示:

data have3;
input ID Vintage;
datalines;
1 2017
1 2018
1 2019
1 2020
2 2017
2 2018
2 2019
2 2020  
3 2017
3 2018
3 2019
3 2020
4 2017
4 2018
4 2019
4 2020 
;
run;
现在的问题是,我处理的数据集如下所示

data newdata;
input ID Type;
datalines;
1 A
1 A
1 A
1 A
1 L
1 L
1 L
1 L
2 A
2 A
2 A
2 A
2 L
2 L
2 L
2 L
;
run;
现在,年份附加在201720182019202020202020上

是否有一种方法可以通过ID和类型在vintage上追加,使上述数据如下所示

data want;
input ID Type Vintage;
datalines;
1 A 2017
1 A 2018
1 A 2019
1 A 2020
1 L 2017
1 L 2018
1 L 2019
1 L 2020
2 A 2017
2 A 2018
2 A 2019
2 A 2020
2 L 2017
2 L 2018
2 L 2019
2 L 2020
;
run;

TIA

对于
类型

data want;
set have3;
by id vintage;

if first.vintage then do;
    do type="A","L";
        output;
    end;
end;
run;

proc sort data=want;
by id type vintage;
run;
SQL解决方案需要一个DISTINCT子句来删除重复项。这是因为交叉联接将创建重复记录,因为
newdata
具有重复项

proc sql noprint;
create table want as
select a.id, b.type, a.vintage
    from have3 as a,
         (select distinct * from newdata) as b
    where a.id = b.id
    order by a.id, b.type, a.vintage;
quit;

因为您希望进行一系列交叉联接,所以只需为每个列创建具有不同值的表,并在procsql中将它们联接在一起

data ids;
input ID ;
datalines;
1
2
3
4
;
run;

data vintages;
input vintage ;
datalines;
2017
2018
2019
2020
;
run;

data types;
input type $;
datalines;
A
L
;
run;

proc sql noprint;
create table have3 as 
select a.id, b.type
from ids a, types b;

create table want as
select a.id, b.type, c.vintage
from ids a, types b, vintages c
order by id, type, vintage;
quit;

不幸的是,它没有达到预期的效果。我得到2017年a型twiceso的想法是,我有一个数据集,有两列ID类型,然后我想分配一个新的列-Vintage ID类型Vintage,这样看起来像是最新的数据集。你在这里运行了这两个示例吗?两者都会生成反映您“想要的”数据的表。我将声明误导have3数据集的责任,我正在寻找创建该数据集。明白了,我发布了一个新的通用解决方案,我认为它会让您的生活更轻松。
data ids;
input ID ;
datalines;
1
2
3
4
;
run;

data vintages;
input vintage ;
datalines;
2017
2018
2019
2020
;
run;

data types;
input type $;
datalines;
A
L
;
run;

proc sql noprint;
create table have3 as 
select a.id, b.type
from ids a, types b;

create table want as
select a.id, b.type, c.vintage
from ids a, types b, vintages c
order by id, type, vintage;
quit;