BookmarkSubscribeRSS Feed
mahi1
Calcite | Level 5

I have a date column in the format YYYYMM and I want to convert that to a number and not sas date number

Example: Date type 201708 should be converted to number 201708

Is there any way to do it?

 

Thanks

2 REPLIES 2
ballardw
Super User

Is your current value actually numeric with a SAS format such as YYMMN6. or similar assigned?

 

Then in a data step

data want;

   set have;

   date = year(date)*100+month(date);

   FORMAT date best6.;

run;

 

is one way. But I would strongly recommend not doing this as date values are ever so much more flexible than plain numeric values when dates are involved.

 

If your "date" is character then there are more gyrations as you cannot change the type of a variable

data want;

   set have (rename=(date=datechar);

   date = input(datechar, f6.);

   drop datechar;

run;

 

But again I would suggest actually creating a SAS date value.

Otherwise we need a better example of your data and what you expect.

Shemp
Obsidian | Level 7

Try this:

 

data test;
   length var_date 8;
   length var_num  8;
   format var_date yymmn6.;
   var_date='01JUN2019'd;
   var_num =input(put(var_date, yymmn6.), 6.);
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
  • 2 replies
  • 5851 views
  • 2 likes
  • 3 in conversation