BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abhi18sas
Calcite | Level 5

Hi,

So, I have a variable named Birthmonth which has Months in Character i.e January, February...etc

I want these to be converted into their respective numeric equals.

How to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  month=month(input(catt('1',substr(birthmonth,1,3),'2018'),date9.));
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  month=month(input(catt('1',substr(birthmonth,1,3),'2018'),date9.));
run;

Art, CEO, AnalystFinder.com

 

abhi18sas
Calcite | Level 5

Thanks!!

ballardw
Super User

Here are two ways:

data want;
   set have;
   select (birthmonth);
      when ('January')   monthnum=1;
      when ('February')  monthnum=2;
      when ('March')     monthnum=3;
      when ('April')     monthnum=4;
      when ('May')       monthnum=5;
      when ('June')      monthnum=6;
      when ('July')      monthnum=7;
      when ('August')    monthnum=8;
      when ('September') monthnum=9;
      when ('October')   monthnum=10;
      when ('November')  monthnum=11;
      when ('December')  monthnum=12;
      otherwise;
   end;
run;

data want;
   set have;
   birthmonth='January';
   array m(12) $ 15 _temporary_('January','February','March','April','May','June','July','August','September'
       'October','November','December') ;
  monthnum = whichc(birthmonth,of m(*));
run;

Both of these are case sensitive. So if your spelling changes case such as "MAY", "May" and "may" you would want to use a case function on your variable (propcase(birthmonth) most likely) and use the appropriate comparison.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 4853 views
  • 2 likes
  • 3 in conversation