SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

User define 2 macro variables in format YYMM.

The task is to create 2 more macro variables that are calculated automatically.

What is the way to calculate them please?

First macro var will get value of start of month of  From_YYMM

Second macro var will get value of End of month of  until_YYMM

 

/***USer Define macro variables***/
%let From_YYMM=2212;
%let until_YYMM=2307;

/***Macro variables that need to be calculated automatically***/
%let FROM='01DEC2022'd;
%let Until='31JUL2023'd;
1 ACCEPTED SOLUTION
2 REPLIES 2
mkeintz
PROC Star

I'd suggest:

%let from_date=%sysfunc(putn(%sysfunc(inputn(20&from_yymm.01,yymmdd8.)),date9.));
%let until_date=%sysfunc(putn(%sysfunc(intnx(month,%sysfunc(inputn(20&until_yymm.01,yymmdd8.)),0,e)),date9.));

which will produce macrovars

GLOBAL FROM_DATE 01DEC2022
GLOBAL FROM_YYMM 2212
GLOBAL UNTIL_DATE 31JUL2023
GLOBAL UNTIL_YYMM 2307

I think eliminating the single quotes makes things simpler.  FROM_DATE and UNTIL_DATE are human-readable, while allowing this usage in your regular SAS code.  Just use double-quotes (which will not mask the value from the macro interpreter) and the trailing letter d.

 

where date between "&from_date"d and "&to_date"d;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 2 replies
  • 531 views
  • 2 likes
  • 3 in conversation