在SAS中合并或连接不等长数据并保留其中一个数据集中的重复值。

huangapple go评论60阅读模式
英文:

merge or join unequal data and duplicate value of one of them in sas

问题

我正在尝试合并两个数据集(df1df2),其中其中一个df2只有一个观察值,我希望将其值分配给df1中的所有重复值,使用sas中的合并操作。

我知道我可以手动添加,但我希望使用自动化的方法,因为这只是我的大型数据代码中的一步。

以下是一个可重现的示例和数据集:

data df1;
input a b c;
datalines;
1 2 3
6 7 8
5 6 9 
;
run;

data df2;
input d ;
datalines;
4  
;
run;

data df3;
merge df1 df2;
run;

/*我需要结果数据集df3如下所示:*/;

a b c d
1 2 3 4
6 7 8 4
5 6 9 4

任何帮助将不胜感激。

英文:

I am trying to merge 2 datasets (df1, df2) with the one of them df2 has only 1 observation that I want to assign its value to all length of the df1 duplicate with merge in sas.
I am aware that I can add that manually but I want to use automated way as this is just a step in my long code with big data.

Here is a reproducible example and datasets:

data df1;
input a b c;
datalines;
1 2 3
6 7 8
5 6 9 
;
run;

data df2;
input d ;
datalines;
4  
;
run;

data df3;
merge df1 df2;
run;

/*I need the resulting df3 to be */;

a b c d
1 2 3 4
6 7 8 4
5 6 9 4

Any help will be greatly appreciated.

答案1

得分: 1

然后你不想合并数据集,因为没有共同的变量可以进行合并。

相反,只需设置两个数据集,但要注意不要读取超出单个观察集的末尾。

data want;
  set long_dataset;
  if _n_=1 then set short_dataset;
run;
英文:

Then you don't want to MERGE the dataset, since there are no common variables that the merge could actually use.

Instead just SET both datasets, but take care to not read past the end of single observation set.

data want;
  set long_dataset;
  if _n_=1 then set short_dataset;
run;

huangapple
  • 本文由 发表于 2023年2月18日 09:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定