简体   繁体   中英

dynamic date range of one year in Proc SQL pass through query

New to SAS and SQL and struggling to create a dynamic range in PROC SQL.

As of now, code is hard coded as -

xxxx.xx_ccyymm_xx between '201607' and '201706'. Trying to create a dynamic range so that next month when it is run, it takes on values as 201608 and 201707.

*the variable stores date as in string format

Someone please suggest a way out!!

Sincere Thanks in advance.

In proc sql :

. . .
where xxxx.xx_ccyymm_xx <= put(today(), yymmn6.) and
      xxxx.xx_ccyymm_xx <= put(intnx('year', today(), 1), yymmn6.)

You can use macro variables to hold the strings. You can use %SYSFUNC() to call the INTNX() function to adjust the date by months. You could use the automatic macro variable &SYSDATE9 to have the day your SAS session started in DATE9 format, or call the DATE() function to get the actual current date.

%let today=%sysfunc(date(),date9);

Then you can generate your YYYYMM format strings from that.

%let current_month=%sysfunc(putn("&today"d,yymmn6));
%let start_month=%sysfunc(intnx(month,"&today"d,-11),yymmn6);

Then in your query use the macro variables

where myvar between "&start_month" and "&current_month"

If you want to have it apply based on run-time:

xxxx.xx_ccyymm_xx between
  left(convert(varchar,dateadd(month,-11,getdate()),112),6)
    and 
  left(convert(varchar,getdate(),112),6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM