DATA Step, Macro, Functions and more

How to calculate monthly and quaterly LAG periods in PROC SQL ?

Reply
Contributor
Posts: 36

How to calculate monthly and quaterly LAG periods in PROC SQL ?

[ Edited ]

I'm just wondering how to calculate the lag dates for previous month and previous quarter in Proc SQL. I'm using following proc sql but I cannot generate the lag period for each period.

 

proc sql;
CREATE TABLE periods_eba as

Select  distinct
CASE WHEN  calendar_day > &qtrly_cutoff_date
	 THEN  'Monthly' ELSE 'Quarterly' END AS Rep_Period,
intnx('month',calendar_day,0,'beginning')  as period format=mmyyd7.,

CASE WHEN calendar_day > &qtrly_cutoff_date
	 THEN intnx('month',calendar_day,0,'beginning') 
	 ELSE intnx('qtr',calendar_day,0,'beginning') END as periodBeg format=yymmdd10., 
    
intnx('month',calendar_day,0,'end')  as periodEnd format=yymmdd10.
from ebaperiod
Order by lagperiod desc
;QUIT;

 

 

 

 

My intention is to create 

 

1) From Today till May 2017  monthly periods

2) From May 2017 to Jan2010 Quarterly periods

final data set will contain lag periods ..and it will look like similar to below.

 

 

Rep_PeriodlagperiodlagperiodBeglagperiodEndperiodperiodBegperiodEnd
Monthly02-20182018-02-012018-02-2803-20182018-03-012018-03-31
Monthly01-20182018-01-012018-01-3102-20182018-02-012018-02-28

 

Or more precisely

 

Rep_PeriodlagperiodlagperiodBeglagperiodEndperiodperiodBegperiodEnd
Monthly02-20182018-02-012018-02-2803-20182018-03-012018-03-31
Monthly01-20182018-01-012018-01-3102-20182018-02-012018-02-28
       
Monthly05-20172017-05-012017-05-3106-20172017-06-012017-06-30
Quarterly03-20172017-01-012017-03-3105-20172017-05-012017-05-31
Quarterly12-20162016-10-012016-12-3103-20172017-01-012017-03-31
       
Super User
Posts: 2,512

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Posted in reply to emrancaan

Your SQL looks fine, What is the issue?

Contributor
Posts: 36

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Posted in reply to emrancaan
I want to genarate three columns 2,3,4 having lag periods
Super User
Posts: 2,512

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Posted in reply to emrancaan

I am unsure why you can't do it. Like this?

 

proc sql;
  create table PERIODS_EBA as
  select  distinct
    ifc(CALENDAR_DAY > &qtrly_cutoff_date., 'Monthly', 'Quarterly') as REP_PERIOD length=9
   ,intnx('month',calendar_day,0 ,'beginning')                      as PERIOD     format=mmyyd7.
   ,intnx('month',calendar_day,-1,'beginning')                      as LAGPERIOD  format=mmyyd7.
        
   ,intnx(ifc(CALENDAR_DAY > &qtrly_cutoff_date.,'month','qtr')
         ,CALENDAR_DAY,0,'beginning')                               as PERIODBEG  format=yymmdd10.
        
   ,intnx('month',CALENDAR_DAY,0,'end')                             as PERIODEND  format=yymmdd10.
  from EBAPERIOD
  ;
quit;

 

REP_PERIOD PERIOD LAGPERIOD PERIODBEG PERIODEND
Monthly 01-2018 12-2017 2018-01-01 2018-01-31
Contributor
Posts: 36

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Thanks Chris, final dataset seems fine but I also need LagperiodBeg and LagperiodEnd. I used lag() but it didn't work.

Super User
Posts: 2,512

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Posted in reply to emrancaan
You can't use lag() in sql. Just use the same logic as for the other fields .
Contributor
Posts: 36

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

I cannot generate he required output fully It is failing at

 

Quarterly03-20172017-01-012017-03-3105-20172017-05-012017-05-31
Super User
Posts: 6,934

Re: How to calculate monthly and quaterly LAG periods in PROC SQL ?

Posted in reply to emrancaan

To get "prior" period dates, use INTNX.  Just change the 0 to -1 to go back one period.

Ask a Question
Discussion stats
  • 7 replies
  • 180 views
  • 1 like
  • 3 in conversation