BC1554
BC1554

Reputation: 137

PROC REPORT within DATA in SAS

I am trying to do a simple thing - write a PROC REPORT procedure within a DATA sentence. My main idea is - if the condition in data step is true - lets execute PROC REPORT, if it is false - do not execute PROC REPORT. Any ideas? Code runs without errors for now, but I see that condition in IF statement is not applied and PROC REPORT is ececute despite the fact that condition is not fulfilled.

Thank you in Advance.

%let DATO    =  13062016;

PROC IMPORT OUT= WORK.auto1 DATAFILE= "C:\Users\BC1554\Desktop\andel.xlsx"
DBMS=xlsx REPLACE;
SHEET="sheet1"; 
GETNAMES=YES;
RUN;


data want;
set WORK.auto1;
rownum=_n_;
run;

DATA tbl2;
SET want;
if (rownum => 1 and rownum <=6 ) then output work.tbl2  ;
RUN;

ODS NORESULTS;
ods LISTING close;
ODS RTF FILE="C:\Users\BC1554\Desktop\Statistik_andel_&DATO..rtf";
title "Statistics from monthly run of DK shares of housing companies (andelsboliger)";
 data Tbl21 ;
 set work.Tbl2; 
 where (DKANDEL='Daekning_pct_24052016' or DKANDEL='Daekning_pct_18042016') ;
 difference = dif(Andel);
 difference1 = dif(Total);
 run;
 data Tbl211 ;
set work.Tbl21; 
where (DKANDEL='Daekning_pct_18042016') ;
run;
data Tbl2111 ;
set work.Tbl211; 
where (DKANDEL='Daekning_pct_18042016') ;
if abs(difference) > 10 and abs (difference1) > 107 then ;
run; 

proc report data= work.Tbl2 spanrows;

columns DKANDEL Andel Total Ukendt  ; 
title2 "-";
title3 "We REPORT numbers on p.4-5".;
title4 "-";
title5 "The models coverage";
title6 "Run date &DATO.";
footnote1 "Assets without currency code not included";
define DKANDEL / order;
define Andel   / order;
define Total   / order;
define Ukendt  / order;
define DKANDEL/ display;
define Andel  / display;
  Compute DKANDEL;
   call define (_col_,"style","style={background=orange}");
    endcomp;
Compute Andel;
        call define (_col_,"style","style={background=red}");
endcomp;
run; title; footnote1;
ODS RTF close;
ODS LISTING;
title;
       run;

Upvotes: 1

Views: 815

Answers (2)

Joe
Joe

Reputation: 63424

Tom's answer is a good one, and probably what I'd do. But, an alternative that is more exactly what you suggested in the question seems also appropriate.

The way you execute a PROC REPORT in a data step (or execute any non-data-step code in a data step) is with call execute. You can use call execute to execute a macro, or just a string of code; up to you how you want to handle it. I would make it a macro, because that makes development much easier (you can write the macro just like regular code, and you can test it independently).

Here's a simple example that is analogous to what Tom put in his answer.

%macro print_report(data=);
  proc report data=&data.;
  run;
%mend print_report;


data _null_;
  set sashelp.class ;
  if age=13 then do;
    call execute('%print_report(data=sashelp.class)');
    stop;  *prevent it from donig this more than once;
  end;
run;

Upvotes: 2

Tom
Tom

Reputation: 51566

To conditionally execute code you need to use a macro so that you can use macro logic like %IF to conditionally generate the code.

But for your simple problem you can use a macro variable to modify the RUN; statement on your PROC REPORT step. Create a macro variable and set it to the value CANCEL when you don't want the step to run.

%let cancel=CANCEL;
...
if abs(difference) > 10 and abs (difference1) > 107 then call symputx('cancel','');
...
proc report ... ;
...
run &cancel ;

Simple example. Produce report if anyone is aged 13.

%let cancel=CANCEL;
data _null_;
  set sashelp.class ;
  if age=13 then call symputx('cancel',' ');
run;
proc report data=sashelp.class ;
run &cancel;

Upvotes: 2

Related Questions