DATA Step, Macro, Functions and more

Performance optimization in sas.

Reply
Occasional Contributor
Posts: 10

Performance optimization in sas.

I have input file of 129632 records that needs to be split on the basis of start date and end date into two file . I have calculated those Date in my first part .I want to use those date just as variable  so that the CPU time decrease. Below code is also working but the output is adding Date with all the input record as observation and hence increase the total turn around time. 

Please suggest.

 

Code :

DATA DATE_CHK;                                                        
IF _N_ = 1 THEN DO;                                                   
END_DATE = TODAY();                                                   
RETAIN END_DATE;                                                      
CURRENT_DAY = DAY(TODAY());                                           
TEMP_DATE=INTNX('MONTH',END_DATE,-3);                                 
START_DATE=TEMP_DATE + CURRENT_DAY - 1;                               
RETAIN START_DATE ;                                                   
END;                                                                  
                                                                      
INFILE INPUT1   ;--FILE HAS 129632 RECORDS                               
INPUT @9 CODEDT $CHARZB10.;                                           
CODE_DATE=INPUT(CODEDT,YYMMDD10.);                                    
FILE ACTIVE;                                                          
IF CODE_DATE >= START_DATE AND CODE_DATE <= END_DATE THEN PUT _INFILE_;
FILE INACTIVE;         

IF CODE_DATE <  START_DATE THEN PUT _INFILE_;
RUN;                                                                  

Esteemed Advisor
Posts: 5,626

Re: Performance optimization in sas.

Just name the variables that you want in your output dataset in a KEEP statement.

 

For example:

 

KEEP code_date start_date end_date;

PG
Occasional Contributor
Posts: 10

Re: Performance optimization in sas.

Thanks
Super User
Posts: 2,513

Re: Performance optimization in sas.

Like this?

data DATE_CHK;                                                        
  retain END_DATE START_DATE;
  drop END_DATE START_DATE CODEDT; 
  format CODE_DATE date9.;
  if _N_ = 1 then do;                                                   
    END_DATE    = today();                                                   
    CURRENT_DAY = day(today());                                           
    TEMP_DATE   = intnx('month',END_DATE,-3);                                 
    START_DATE  = TEMP_DATE + CURRENT_DAY - 1;                               
  end;                                                                  
  infile INPUT1;                
  input @9 CODEDT $charzb10.;                                           
  CODE_DATE = input(CODEDT,yymmdd10.);                                    
  if CODE_DATE >= START_DATE and CODE_DATE <= END_DATE then do;
    file ACTIVE;                                                          
    put _INFILE_;
  end;
  if CODE_DATE <  START_DATE then do;
    file INACTIVE;         
    put _INFILE_;
  end;
run;              

1. All uppercase is hard to read

2. data _null_ ;  will further speed things up, if you only need the new flat files

3. Moving the file statements in the do block should save time 

4. 100k records should take no time at all

 

 

 

Occasional Contributor
Posts: 10

Re: Performance optimization in sas.

Thanks , but still the CPU is not decreasing.
Respected Advisor
Posts: 4,797

Re: Performance optimization in sas.

@WFC2013

129632 records is low volume so not sure why you care that much about resources unless that's a process which you run every 5 minutes or so.

 

To consume less resources:

- Use a Data _NULL_; step

- Use if... then... ELSE as you're either writing a record to the active or the inactive file but not both.

 

And last but not least: If changing between two output files don't you need to use the MOD option in your file statements. Does your code actually work?

Super User
Posts: 2,513

Re: Performance optimization in sas.

We are running out of options here.

@Patrick is right that adding else will shave a few μs, but that's it.

How much CPU are you using? How much are you aiming for?

Show us the full log please, with options source and fullstimer turned on.

 

Super User
Posts: 2,513

Re: Performance optimization in sas.

You may also be able to save peanuts by removing 2 lines of code:

  START_DATE  = intnx('month', END_DATE, -3, 's');                                

 

 

Ask a Question
Discussion stats
  • 7 replies
  • 124 views
  • 2 likes
  • 4 in conversation