[英]How do i assign a value to a new variable, using another dataset which contains one value in SAS
I have a dataframe 我有一个数据框
ID value1
1 12
2 345
3 342
i have a second dataframe 我有第二个数据框
value2
3823
how do I get the following result? 如何获得以下结果?
ID value1 value2
1 12 3823
2 345 3823
3 342 3823
any joins I have done have given me 我所做的任何加入都给了我
ID value1 value2
1 12 .
2 345 .
3 342 .
. . 3823
No need for joins or helper variables: 无需联接或辅助变量:
data have;
do i = 1 to 3;
output;
end;
run;
data lookup;
j = 1;
run;
data want;
set have;
if _n_ = 1 then set lookup;
run;
Without the if _n_ = 1
, the data step stops after one iteration when it tries to read a second row from the lookup dataset and finds that there are no rows remaining. 如果
if _n_ = 1
,则当数据步骤尝试从查找数据集中读取第二行并发现没有剩余行时,它会在一次迭代后停止。
NB this requires that the have
dataset doesn't already contain a variable with the same name as the variable(s) attached from the lookup dataset. 注意,这要求
have
数据集尚未包含与查找数据集附加的变量名称相同的变量。
By far the easiest way to do this is to utilize PROC SQL and defining the condition 1=1, which is always true for each comparison: 到目前为止,最简单的方法是利用PROC SQL并定义条件1 = 1,这对于每次比较总是正确的:
data first;
input ID value1 @@;
cards;
1 12 2 345 3 342
run;
data second;
input value2 ;
cards;
3823
run;
proc sql;
create table wanted as
select * from first
left join second
on 1 =1
;quit;
Edit: As far as I know, there isn't direct way to merge datasets by each row, but you can do the following trick: 编辑:据我所知,没有直接合并每一行数据集的方法,但是您可以执行以下技巧:
Add variable Help: 添加变量帮助:
data second_trick;
set second;
help=1;
run;
data first_trick;
set first;
help=1;
run;
Then we just perform the merge by the static variable: 然后,我们仅通过静态变量执行合并:
data wanted_trick;
merge first_trick(in=a) second_trick;
by help;
if a; /*Left join, just to be sure.*/
run;
now this only works if you want to add single static value. 现在这仅在您要添加单个静态值时有效。 Don't try to use it your Second set has more rows.
不要尝试使用它, 第二组有更多行。
For more on Merges and joins see: https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf 有关合并和联接的更多信息,请参见: https : //support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.