If you really want to define a format:
proc fcmp outlib=WORK.FUNCTIONS.CUSTOM;
function yyhh(DATE) ;
return (put(DATE,year4.)||put(ceil(qtr(DATE)/2.1),z2.));
endsub;
run;
options cmplib=WORK.FUNCTIONS;
proc format;
value yyhh (default=6) '01jan1600'd - '01jan20000'd = [yyhh()]
other = 'Invalid date';
run;
data _null_;
YYHH = put('01sep2018'd, yyhh.) ;
put YYHH=;
run;
YYHH=201802
Personally, I'd create the format as 2018H2 to avoid any confusion with months or quarters.
Ha, I wanted to show off and support 5-digit years but forgot bits here and there...
This works even better:
proc fcmp outlib=WORK.FUNCTIONS.CUSTOM;
function yyhh(DATE) ;
return (put(DATE,year5.)||put(ceil(qtr(DATE)/2.1),z2.));
endsub;
run;
options cmplib=WORK.FUNCTIONS;
proc format;
value yyhh (default=6) '01jan1600'd - '01jan20000'd = [yyhh()]
other = 'NoDate';
run;
data _null_;
YYHH = put('01sep12018'd, yyhh7.) ;
put YYHH=;
YYHH = put('01sep2018'd, yyhh.) ;
put YYHH=;
run;
YYHH=1201802
YYHH=201802
That worked! Thank you, @ChrisNZ !
And thanks again to everyone who tried to help, especially @novinosrin!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.