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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.