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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6956 views
  • 2 likes
  • 3 in conversation