BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cm3
Fluorite | Level 6 cm3
Fluorite | Level 6

I have a variable Enrollment_month - type character, length 8 with value ex:FEB2022. I want to convert this into date format as 20220201. Please help

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star
data Want;
  Enrollment_month = 'FEB2022';
  Enrollment_date = input('01' !! Enrollment_month, date9.);
  format Enrollment_date yymmddn8.;
  put _all_;
run;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Since your notation fits most of the DATE9 informat, conversion is easy:

data want;
set have (rename=(enrollment_month=_em));
format enrollment_month yymmddn8.;
enrollment_month = input(cats("01",_em),date9.);
drop _em;
run;

 

PS I changed your subject line. "Hi" is not descriptive and only makes you look silly.

cm3
Fluorite | Level 6 cm3
Fluorite | Level 6
your solution worked , thank you.
Thanks for updating the title, it was bit confusing in mobile and I missed it
SASKiwi
PROC Star
data Want;
  Enrollment_month = 'FEB2022';
  Enrollment_date = input('01' !! Enrollment_month, date9.);
  format Enrollment_date yymmddn8.;
  put _all_;
run;
cm3
Fluorite | Level 6 cm3
Fluorite | Level 6
Also I was wondering how would we convert if enrollment_month is type date with same value?? Pls confirm
Kurt_Bremser
Super User

If it is already a SAS date value, you only need to change the assigned format, e.g. with PROC DATASETS for the dataset, or with a FORMAT statement (or FORMAT= option in PROC SQL) wherever needed.

SASKiwi
PROC Star

"If enrollment_month is type date with same value" - A numeric variable can't contain the same value as "FEB2022" as that is character data. The closest you can get is this, where you define it as a date based on 01 Feb 2022:

data Want;
  Enrollment_month = '01FEB2022'd;
  Enrollment_date = Enrollment_month; 
  format Enrollment_month date9. Enrollment_date yymmddn8.;
  put _all_;
run;
Kurt_Bremser
Super User

PS there is no type "date" in SAS. SAS has only two types of variables, character and numeric.

Dates are stored in numeric variables, as counts of days with 1960-01-01 as day zero, and with a proper format assigned to make the raw value human-readable.

Ksharp
Super User
Also could try MONYY. informat.

data Want;
Enrollment_month = 'FEB2022';
Enrollment_date = input(Enrollment_month, monyy9.);
format Enrollment_date yymmddn8.;
put _all_;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 629 views
  • 3 likes
  • 4 in conversation