Pro c Отчет по убыванию - PullRequest
       69

Pro c Отчет по убыванию

0 голосов
/ 26 мая 2020

У меня есть следующий код, результат - sas. Он сортирует по убыванию по промежуточным итогам, однако мне нужен отчет, чтобы упорядочить его по убыванию по офицерам в рамках того же промежуточного итога. Код прилагается. Как мне это сделать?

data have;
    format setup_date date9.;
      infile datalines;
        input setup_date date9. Division $ Officer $ cnt;
return;
datalines;
1Jun2018 Central Smith 1
10Jun2018 Central Smith 1
10Jul2018 Central Smith 1
20Jun2018 Central Smith 1
11Jun2018 Central Shelton 1
1May2018 Central Baldwin 1
16May2018 Central Stone 1
12May2018 Central Grant 1
14May2018 Central Grant 1
1Sep2018 Central Jones 1
11Apr2019 Atlantic James 1
17Apr2019 Atlantic James 1
19Apr2019 Atlantic Smith 1
1Feb2019 Atlantic Doglass 1
14Feb2019 Atlantic Shane 1
15Feb2019 Atlantic Shane 1
16Feb2019 Atlantic Shane 1
17Feb2019 Atlantic Shane 1

;run;

proc sql;
  create table report_data as
  select *,
       sum(cnt) as div_cnt_sum 
  from have 
       group by division;
;quit;

proc report data=report_data wrap style(summary)=Header;
    columns div_cnt_sum Division WiderDivision Officer setup_date cnt ;

  define div_cnt_sum / group descending noprint ;                      
  define Division / group noprint;                                    
  define WiderDivision / 'Division' computed; 
  define Officer  /group ;          
  define setup_date / across format=year4. order=internal '';          
  define cnt / sum f=comma6. 'Row Tot';                                

  break after Division / summarize;
      rbreak after / summarize;

  compute WiderDivision / character length=25;                            /* specify the widerness */
    WiderDivision = Division;
  endcomp;

  compute after;
    WiderDivision='Grand Total';
  endcomp;
run;

Я хочу сохранить общую дивизию и отсортировать по офицерам

1 Ответ

1 голос
/ 26 мая 2020

Вы должны были предварительно вычислить сумму division, чтобы упорядочить по сумме группы. Теперь вы хотите дополнительно заказать по сумме группы более officer в пределах division. Для того, чтобы (предназначенный каламбур) это сделать, вам нужно будет придерживаться того же подхода. Предварительно вычислите суммы каждого division officer по групповой комбинации и используйте их в другом noprint столбце отчета.

Пример:

data have;
  format setup_date date9.;
  infile datalines;
  input setup_date date9. Division $ Officer $ cnt;
datalines;
1Jun2018 Central Smith 1
10Jun2018 Central Smith 1
10Jul2018 Central Smith 1
20Jun2018 Central Smith 1
11Jun2018 Central Shelton 1
1May2018 Central Baldwin 1
16May2018 Central Stone 1
12May2018 Central Grant 1
14May2018 Central Grant 1
1Sep2018 Central Jones 1
11Apr2019 Atlantic James 1
17Apr2019 Atlantic James 1
19Apr2019 Atlantic Smith 1
1Feb2019 Atlantic Doglass 1
14Feb2019 Atlantic Shane 1
15Feb2019 Atlantic Shane 1
16Feb2019 Atlantic Shane 1
17Feb2019 Atlantic Shane 1
;

* nested queries for computing sums of two different by groups;
* grouping by division is the nested sub-query and a new outer scope 
* computes the sums for division,officer;

proc sql;
  create table report_data as
  select *,
    sum(cnt) as div_off_cnt_sum
  from
  (
    select *,
         sum(cnt) as div_cnt_sum 
    from have 
         group by division
  )
  group by division, officer   
  ;
quit;

proc report data=report_data wrap style(summary)=Header;
    columns 
        div_cnt_sum       /* to be used for ordering by group sum */
      Division
      WiderDivision  

        div_off_cnt_sum   /* officer within division sum(cnt) */
      Officer

      setup_date 
      cnt 
    ;

  /* precomputed group sums for custom ordering by totals when /across in effect */ 
  define div_cnt_sum     / group descending noprint;
  define div_off_cnt_sum / group descending noprint;

  define Division / group noprint;
  define WiderDivision / 'Division' computed;
  define Officer / group descending;
  define setup_date / across format=year4. order=internal '';

  define cnt / sum f=comma6. 'Row Tot';

  break after Division / summarize;
      rbreak after / summarize;

  compute WiderDivision / character length=25;
    WiderDivision = Division;
  endcomp;

  compute after;
    WiderDivision='Grand Total';
  endcomp;
run;

enter image description here

...