Hello Experts,
My code is :
AnneeSemaine=compress(year(datepart (MaDate))||"-"||week(datepart(MaDate),'v'));
I have the value 2023-9, do you know please some easy option to get 2023-09?
My second line of code is :
Moisjour=compress(month(datepart (MaDate))||"-"||day(datepart(MaDate)));
I have the value 9-2, do you know please how to get the value 09-02 ?
Thank you for your help !
First use one of the new CAT series of functions so you don't need COMPRESS().
Second take control over how the number is converted to a string before the concatenation.
Example:
AnneeSemaine=catx('-',put(datepart(MaDate),year4.),put(week(datepart(MaDate),'V'),Z2.));
Moisjour=catx('-',put(month(datepart(MaDate)),Z2.),put(day(datepart(MaDate)),Z2.));
Or just use the right FORMAT to generate the string directly from the DATE value.
AnneeSemaine=compress(put(datepart(MaDate),yyweekv8.),'W');
Moisjour=substr(put(datepart(MaDate),yymmddd10.),6);
For the first, use the YYWEEKV format, and either (with a format length of 7) replace the W with a dash, or (with a length of 😎 COMPRESS the W away.
For the second, roll your own formt with PROC FORMAT. Specify a DATATYPE of datetime, so you can even omit the DATEPART function.
First use one of the new CAT series of functions so you don't need COMPRESS().
Second take control over how the number is converted to a string before the concatenation.
Example:
AnneeSemaine=catx('-',put(datepart(MaDate),year4.),put(week(datepart(MaDate),'V'),Z2.));
Moisjour=catx('-',put(month(datepart(MaDate)),Z2.),put(day(datepart(MaDate)),Z2.));
Or just use the right FORMAT to generate the string directly from the DATE value.
AnneeSemaine=compress(put(datepart(MaDate),yyweekv8.),'W');
Moisjour=substr(put(datepart(MaDate),yymmddd10.),6);
And here's a code example:
proc format;
picture mmdd
low-high = '%0m-%0d' (datatype=datetime)
;
run;
data want;
now = datetime();
year_week = compress(put(datepart(now),yyweekv8.),"W");
month_day = put(now,mmdd.);
format now datetime19.;
run;
The same idea with Kurt.
proc format;
picture mmdd
low-high = '%0Y-%0V' (datatype=datetime)
;
picture mm
low-high = '%0Y-%0W' (datatype=datetime)
;
picture m
low-high = '%0Y-%0U' (datatype=datetime)
;
run;
data want;
now = datetime();
format now mmdd7.;
_now ='01jan2024:10:01:10'dt;
format _now mm7.;
_now2 ='01jan2024:10:01:10'dt;
format _now2 m7.;
run;
proc print;run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.