SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

How to convert a number into date, say the no is 22625 which I want to show as 11Dec2021, I read the doc but couldnt get it. Thanks.

1 REPLY 1
ballardw
Super User

I think all you need to do is apply a DATE9. format. If you do this as part of a data step then the format is now the default display format.

 

data example;
   x=22625;
   format x date9. ;
run;

You can assign a different format for specific uses:

Title "Default format assigned";
proc print data=example;
run;

Title "Override default format in procedure";
proc print data=example;
   format x yymmdd10.;
run;
title;

SAS formats are rules for how to display any given value(s). Note that the the name of the format ends in a dot, or would have a dot where decimals would occur. In the following example the number will be displayed with at most 12 characters including a decimal point and 2 decimals, which here forces zeroes after the decimal.

Proc print data=example;
    format x f12.2;
run;

Formats are a very powerful tool in SAS as groups created by formats are used by most analysis, graphing and reporting procedures. So if you have a lot of date values and want to know how many values are in each year you could use Proc freq and a format that only displays years:

Proc freq data=yourdataset;
   tables datevariable;
   format datevariable year4. ;
run;

Or pick a format that only displays year and month to get the count in each calendar month.

 

Since you can create your own formats with Proc Format the are very few limits on grouping with formats. Plus a few procedures can use special Multiple label formats so that one format could create things like age group 1 to 18, 1 to 6,7 to 12, 13 to 18, 1 to 3, 4 to 6, 7 to 9, 10 to 13, 10 to 18, for example with a single format. Again only Procs Tabulate, Report, Means and Summary will use these but can be very helpful.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 540 views
  • 3 likes
  • 2 in conversation