简体   繁体   English

SAS桌键无钥匙

[英]SAS table joint without key

I have two tables, A and B, B is 1 x 4 tables (Type, Freq, max, min) which is the output from proc mean. 我有两个表,A和B,B是1 x 4个表(类型,频率,最大,最小),这是proc平均值的输出。 I want to add max and min to Table A, for each record, so there is no statement like on a.key=b.key, because I just want to add columns of the same numbers to table A. What should I do? 我想为每条记录的表A添加最大和最小值,因此没有像a.key = b.key这样的语句,因为我只想向表A添加相同编号的列。我该怎么办?

You can add summary statistics into your original data set in two ways, one is using PROC SQL and the other uses a DATA step. 您可以通过两种方式将摘要统计信息添加到原始数据集中,一种是使用PROC SQL,另一种是使用DATA步骤。 There are two examples below, one that considers if you have an additional grouping variable, ie you want to join the totals for all Cars of a specific Origin with the group average. 下面有两个示例,其中一个示例考虑是否还有其他分组变量,即您想将特定来源的所有汽车的总数与组平均值相加。

Here's a demo of both approaches, this adds in the average value, but you can expand it to account for other statistics easily. 这是这两种方法的演示,这会增加平均值,但是您可以扩展平均值以轻松说明其他统计信息。

******************************************************;
*Add average value to a dataset;
*Solution 1 - PROC MEANS + Data step;
******************************************************;

proc means data=sashelp.class noprint;
    output out=avg_values mean(height)=avg_height;
run;

data class_data;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;

proc print data=class;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class;
quit;

******************************************************;
*Add average value to a dataset - with grouping variables;
*Solution 1 - PROC MEANS + Data step;
******************************************************;
proc means data=sashelp.class noprint nway;
class sex;
    output out=avg_values mean(height)=avg_height;
run;

*sort data before merge;
proc sort data=sashelp.class out=class;
by sex;
run;

data class_data;
 merge class avg_values;
 by sex;


run;

proc print data=class_data;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class
group by sex;
quit;

The standard way to add single variables to all columns, would not be a merge but a set statement. 将单个变量添加到所有列的标准方法不是合并,而是set语句。 This takes advantage of the fact that, by default, SAS will RETAIN all variables in a dataset that come in via SET/MERGE/UPDATE; 这利用了以下事实:默认情况下,SAS将保留通过SET / MERGE / UPDATE进入的数据集中的所有变量; so long as you use branching (IF/THEN) to prevent SAS from attempting to read the dataset when it is empty, it will do what you want. 只要您使用分支(IF / THEN)来防止SAS在数据集为空时尝试读取它,它就会做您想要的事情。

So for example, you have SASHELP.CLASS, and you want to append the mean height/weight. 例如,您有SASHELP.CLASS,并且想要附加平均身高/体重。 You first have a proc means : 您首先具有proc means

proc means data=sashelp.class noprint;
  var height weight;
  output out=class_means mean= /autoname;
run;

And then you use the set statement, with an if _n_ = 1 clause around it to make sure it's only executed in the first iteration; 然后,使用set语句,并在其周围加上if _n_ = 1子句,以确保仅在第一次迭代中执行该语句。 if you leave that off, SAS will fail to find a second observation in that data set and terminate the datastep before you were expecting (try it!). 如果不进行此设置,SAS将无法在该数据集中找到第二个观测值,并在您期望之前终止数据步骤(尝试!)。

data class_with_mean;
  set sashelp.class;
  if _n_=1 then set class_means(keep=weight_mean height_mean);
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM