简体   繁体   English

如何使用SAS中包含一个值的另一个数据集为新变量分配值

[英]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.

相关问题 如何将一个变量的 .2f 值分配给另一个变量? - How can I assign value of .2f value of one variable to another variable? 如何将变量的值分配给之前的另一个变量? - How can I assign a variable's value to another variable which comes before? 如何增加我在响应中获得的变量并在下一个请求JMETER中分配新值 - How to increase variable which I get in response and assign new value in next request JMETER 如何根据包含另一个(不断增加的)类变量的一组公式增加一个变量整数的值? - How do I increase the value of one variable integer according to a set formula which includes another (constantly increasing) class variable? 如何从变量中获取值并将其分配为新的对象名称? - How do I take a value from variable and assign it asa a new object name? 如何将值从一种形式传递给另一种形式的变量,而该变量实际上改变了变量的默认值? - How do I pass a value from one form to a variable in another that actually changes the default value of the variable? 如何在TSQL中为变量分配JSON值? - How do I assign a JSON value to a variable in TSQL? 如果未分配任何值,如何为变量分配值,但是如果分配了一个值,则如何保留该值? - How do you assign a value to a variable if no value is assigned, but, keep the value if one is assigned? 如何在JavaScript中将变量的值分配给另一个方法? - How to assign value of variable to another method in javascript? 如何将变量分配给 eval / exec 值? - How do i assign a variable to a eval / exec value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM