简体   繁体   中英

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. 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?

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. 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. This takes advantage of the fact that, by default, SAS will RETAIN all variables in a dataset that come in via 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.

So for example, you have SASHELP.CLASS, and you want to append the mean height/weight. You first have a 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; 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!).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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