Larry
Larry

Reputation: 183

Convert month (date) into full month (string)

I have a date field (adate10) called Period with values like 07/02/2018.

I need to convert this into a string like "July 2, 2018."

I can use the code below to produce a string, Period_String, that says "JUL 2, 2018." I can't figure out how to format the month as the full month, e.g., "July." Is the only option to create an if/then statement that says 'if 1 then "January", if 2, then "February"', etc.? Was hoping there was a built in format but can't find it.

* extract each date element, then format.
compute mo = xdate.month(Period).
compute da = xdate.mday(Period).
compute yr = xdate.year(Period).

formats mo (month) da yr (F4.0).
execute.

* concatentate date elements as strings.
string Period_String (a30).
compute Period_String = concat(ltrim(string(mo,month)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
execute.

Upvotes: 2

Views: 414

Answers (1)

eli-k
eli-k

Reputation: 11350

see the revised month format (month9 instead of month):

compute Period_String = concat(ltrim(string(mo,month9)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).

EDIT:
using @mirirai's one-liner suggestion + getting only first letter in capitals:

string Period_String(a20).
COMPUTE Period_String = CONCAT(
            RTRIM(char.substr(STRING(period,MONTH9),1,1)),
            RTRIM(lower(char.substr(STRING(period,MONTH9),2)))," ",
            LTRIM(STRING(XDATE.MDAY(Period),F2)), ", ", 
            LTRIM(STRING(XDATE.YEAR(period),F4))
            ).

Upvotes: 1

Related Questions