BookmarkSubscribeRSS Feed
emrancaan
Obsidian | Level 7

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
       
7 REPLIES 7
emrancaan
Obsidian | Level 7
I want to genarate three columns 2,3,4 having lag periods
ChrisNZ
Tourmaline | Level 20

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
emrancaan
Obsidian | Level 7

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

ChrisNZ
Tourmaline | Level 20
You can't use lag() in sql. Just use the same logic as for the other fields .
emrancaan
Obsidian | Level 7

I cannot generate he required output fully It is failing at

 

Quarterly03-20172017-01-012017-03-3105-20172017-05-012017-05-31
Astounding
PROC Star

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3455 views
  • 1 like
  • 3 in conversation