0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SASテクニック 存在しない水準も含めた要約統計量のデータ作成 classdata/SQL

Last updated at Posted at 2021-06-29

SASによる存在しない水準も含めた要約統計量のデータ作成

SASHELP.CLASSで、年齢/性別ごとに集計する
16歳は、男性のみのデータとなっている

class.PNG

年齢/性別のデータ作成(meansプロシジャで利用)

proc Sql;
  create table work.CD as
    select A.AGE , B.SEX
    from ( select distinct AGE from sashelp.CLASS ) as A
    cross join ( select distinct SEX from sashelp.CLASS ) as B
    order by A.AGE , B.SEX;
quit;

cd.PNG

classdataの指定せず、シンプルに集計

%* classdataなし *;
proc Means data = sashelp.CLASS nway noprint;
  class AGE SEX;
  var HEIGHT;
  output out = work.STAT0 n = N mean = MEAN;
run;

stat0.PNG

16歳女性のレコードは作成されない

classdataを指定して16歳女性のレコードを作る場合

%* classdataあり *;
proc Means data = sashelp.CLASS classdata = work.CD nway noprint;
  class AGE SEX;
  var HEIGHT;
  output out = work.STAT1 n = N mean = MEAN;
run;

集計結果とダミーデータをmergeすることで16歳女性のレコードを作る場合

%* sqlで *;
proc Sql;
  create table work.STAT2 as
    select A.* , coalesce( B.N , 0 ) as N , B.MEAN
    from
      ( select *
        from ( select distinct AGE from sashelp.CLASS )
        cross join ( select distinct SEX from sashelp.CLASS )
      ) as A
    left join
      ( select AGE , SEX , count(*) as N , mean( HEIGHT ) as MEAN
        from sashelp.CLASS
        group by AGE , SEX
      ) as B
    on A.AGE = B.AGE and A.SEX = B.SEX
    order by A.AGE , A.SEX
 ;
quit;

stat1_2.PNG

16歳女性のレコードも含まれる(N=0,MEAN=欠損)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?