<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting date formatted date9. to create substrings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778030#M247626</link>
    <description>&lt;P&gt;Run a PROC CONTENTS on your SAS dataset (not on your Excel output) and see what TYPE of variable you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If DATE is a numeric variable that is being displayed using the DATE9. format then it contains the number of days since 1960.&amp;nbsp; Do not use string functions like SUBSTR() on numeric variables.&amp;nbsp; SAS will first have to convert the number of days into a string and it will not look like the strings that the DATE9. format prints. If you want to get a string that just has the first 5 characters when printed using the DATE9. format then use the PUT() function with the DATE format but set the width to only 5 characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;daymon = put(date,date5.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you want the truncated ddMON strings to display in printouts and reports then don't bother the make any new variables, just use the one you already have with the different display format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format date date5.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If DATE is a character variable that has strings that look like they were dates printed using the DATE9. format then just use the substr() function directly on the character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;daymon = substr(date,1,5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can also use a format to have it only print the first 5 characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format date $5. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have a character variable you can use the INPUT() function to generate a date value from it.&amp;nbsp; Which you can then use with any of the various functions that operate on date values or formats that display date values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;datenum=input(date,date9.);
day_of_month=day(datenum);
month_of_year=month(datenum);
calendar_year=year(datenum);
format datenum yymmdd10.;
format datenum mmddyy10.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PUTN() function makes it possible to pass in the format specification as a string so you can take a look at how a number of different formats will display the same value.&lt;/P&gt;
&lt;PRE&gt;779   data _null_;
780     length format display $50 ;
781     date=today();
782     do format='yymmdd10.','mmddyy10.','ddmmyy10.','date5.','date7.','date9.','date11.','monyy7.' ;
783        display = putn(date,format);
784        put format $12. display ;
785     end;
786   run;

yymmdd10.   2021-11-02
mmddyy10.   11/02/2021
ddmmyy10.   02/11/2021
date5.      02NOV
date7.      02NOV21
date9.      02NOV2021
date11.     02-NOV-2021
monyy7.     NOV2021
&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Nov 2021 21:38:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-11-02T21:38:55Z</dc:date>
    <item>
      <title>Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778024#M247622</link>
      <description>&lt;P&gt;I have data that I am trying to transform in order to create a SAS visual. There are several steps before arriving there.&amp;nbsp; I have exported a few lines of data as an xlsx file and attached here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A typical line of data is like (I have chosen just the important fields)&lt;/P&gt;
&lt;P&gt;Case&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Contact_ID &amp;nbsp; &amp;nbsp; &amp;nbsp; Date1&lt;/P&gt;
&lt;P&gt;234243&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 01MAR2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 51954633&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 03SEP2020&lt;/P&gt;
&lt;P&gt;716432&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 02MAR2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 53426754&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 03SEP2020&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on&lt;/P&gt;
&lt;P&gt;The date values are formatted as date9.&amp;nbsp; I want to split then by day, month, year so I tried the following&lt;/P&gt;
&lt;PRE&gt;Data SASCHART.Date_Fix;
  set SASCHART.Cases_Contacts_Want;
  Yr_1 = substr(Date,6,4); 
  Mon_1= substr(Date,3,5);
  Day_1 = substr(Date,1,2);
  Daymon_1=trim(Day_1||Mon_1);

  Yr_2 = substr(Date1,6,4); 
  Mon_2= substr(Date1,3,5);
  Day_2 = substr(Date1,1,2);
  Daymon_2=trim(Day_2||Mon_2);
run;&lt;/PRE&gt;
&lt;P&gt;which didn't produce the result I need which is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;U&gt;Daymon_1&amp;nbsp;&lt;/U&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;U&gt;Daymon_2&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 01MAR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 03SEP&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on.&amp;nbsp; So I looked in some older SAS Communities posts and tried something like this&lt;/P&gt;
&lt;PRE&gt;Data SASCHART.Date_Fix;
  set SASCHART.Cases_Contacts_Want;
  new_date_1 = mdy(substr(Date,3,5), substr(Date,1,2), substr(Date,6,4));
  format new_date_1 YYMMDD8.;
run;&lt;/PRE&gt;
&lt;P&gt;That didn't work either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to count the number of Cases for each daymon_1&amp;nbsp; and the number of contact_ID by daymon_2.&amp;nbsp; It may be something like&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cases&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; contacts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; count &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 01MAR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 01SEP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 38&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26OCT&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 834&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 16OCT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 543&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I will use the dates on an x-axis and the counts on the y-axis to create an overlaid line graph in SAS.&amp;nbsp;&amp;nbsp; The cases started earlier than contact-tracing and are several days ahead of the contact counts, since contacting_tracing is slowing down for several reasons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, I need to split the dates into substrings properly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your expertise is very much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wlierman&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 20:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778024#M247622</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2021-11-02T20:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778027#M247623</link>
      <description>&lt;P&gt;In SAS data set SASCHART.Cases_Contacts_Want, are the dates numeric or text? What does PROC CONTENTS say?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:03:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778027#M247623</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-02T21:03:53Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778028#M247624</link>
      <description>Use date functions, YEAR(), MONTH(), DAY() to extract the components or look at the SAS formats. &lt;BR /&gt;Or you need to convert it to text if you need it.</description>
      <pubDate>Tue, 02 Nov 2021 21:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778028#M247624</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-02T21:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778029#M247625</link>
      <description>&lt;P&gt;If a value is a DATE you can get the day of the month using the DAY function&amp;nbsp; : dayofmonth= day(datevariable).&lt;/P&gt;
&lt;P&gt;Same with the Month and Year. The values will be numbers, which means the results will sort properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However if you want to Count something in ddMON appearance use Proc Freq and the format DATE5.&lt;/P&gt;
&lt;P&gt;Groups created by formats will be honored for most analysis, reporting and graphing procedures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example:&lt;/P&gt;
&lt;PRE&gt;data example;
   input date :Date9.;
   format date date9.;
datalines;
01Jan2021
05Jan2021
19Jan2021
01Feb2021
01Feb2021
01Feb2020
;

proc freq data=example;
   tables date;
   format date date5.;
run;&lt;/PRE&gt;
&lt;P&gt;Please note that I included a value of the same date in a separate year intentionally. You have to consider year somewhere if your data includes more than one year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This "does not work" because when date is numeric SAS will convert the number to a character value using the BEST12 format resulting in many leading blanks and the substr function has nothing to work with for most of the bits. Plus the MDY function would be attempting to create a new date value, not a string. If Date started as string that might work if starting at the 3rd position the 5 following characters consisted of spaces and or 2 consecutive digits to indicate month but since you have subst(date,6,4) one suspects what ever you borrowed this from was either different or looking a very odd "date" values as character.&lt;/P&gt;
&lt;PRE&gt;  new_date_1 = mdy(substr(Date,3,5), substr(Date,1,2), substr(Date,6,4));&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778029#M247625</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-02T21:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778030#M247626</link>
      <description>&lt;P&gt;Run a PROC CONTENTS on your SAS dataset (not on your Excel output) and see what TYPE of variable you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If DATE is a numeric variable that is being displayed using the DATE9. format then it contains the number of days since 1960.&amp;nbsp; Do not use string functions like SUBSTR() on numeric variables.&amp;nbsp; SAS will first have to convert the number of days into a string and it will not look like the strings that the DATE9. format prints. If you want to get a string that just has the first 5 characters when printed using the DATE9. format then use the PUT() function with the DATE format but set the width to only 5 characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;daymon = put(date,date5.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you want the truncated ddMON strings to display in printouts and reports then don't bother the make any new variables, just use the one you already have with the different display format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format date date5.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If DATE is a character variable that has strings that look like they were dates printed using the DATE9. format then just use the substr() function directly on the character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;daymon = substr(date,1,5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can also use a format to have it only print the first 5 characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format date $5. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have a character variable you can use the INPUT() function to generate a date value from it.&amp;nbsp; Which you can then use with any of the various functions that operate on date values or formats that display date values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;datenum=input(date,date9.);
day_of_month=day(datenum);
month_of_year=month(datenum);
calendar_year=year(datenum);
format datenum yymmdd10.;
format datenum mmddyy10.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PUTN() function makes it possible to pass in the format specification as a string so you can take a look at how a number of different formats will display the same value.&lt;/P&gt;
&lt;PRE&gt;779   data _null_;
780     length format display $50 ;
781     date=today();
782     do format='yymmdd10.','mmddyy10.','ddmmyy10.','date5.','date7.','date9.','date11.','monyy7.' ;
783        display = putn(date,format);
784        put format $12. display ;
785     end;
786   run;

yymmdd10.   2021-11-02
mmddyy10.   11/02/2021
ddmmyy10.   02/11/2021
date5.      02NOV
date7.      02NOV21
date9.      02NOV2021
date11.     02-NOV-2021
monyy7.     NOV2021
&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778030#M247626</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-02T21:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778031#M247627</link>
      <description>&lt;P&gt;You could do it with user define format,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you input date data are text strings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
  input Case Date : $ 9. Contact_ID Date1 : $ 9.;
cards;
234243       01MAR2020      51954633      03SEP2020
716432       02MAR2020      53426754      03SEP2020
;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;do it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
picture mydate (default=5)
          other='%d%b' (datatype=date LANGUAGE=English);
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;[edit:] or use suggested date5. format&lt;/P&gt;
&lt;P&gt;and then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  set have2;
  length Daymon_1 Daymon_2 $ 5;
  Daymon_1 = put(input(Date,date9.), mydate. /* date5. */);
  Daymon_2 = put(input(Date1,date9.),mydate. /* date5. */);
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778031#M247627</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-11-02T21:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778036#M247629</link>
      <description>The dates are numeric from Proc Contents.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;|  Alphabetic List of Variables and Attributes  |&lt;BR /&gt;|  Variable  |  Type  |  Len  |  Format  |  Informat  |  Label  | &amp;nbsp; |&lt;BR /&gt;|  CaseID  |  Num  |  8  |  BEST.  |  &amp;nbsp;  |  CaseID  | &amp;nbsp; |&lt;BR /&gt;|  CaseStatus  |  Char  |  11  |  $11.  |  $11.  |  CaseStatus  | &amp;nbsp; |&lt;BR /&gt;|  CaseStatus1  |  Char  |  11  |  $11.  |  $11.  |  CaseStatus1  | &amp;nbsp; |&lt;BR /&gt;|  Contact_Person_ID  |  Char  |  11  |  $11.  |  $11.  |  Contact Person ID  | &amp;nbsp; |&lt;BR /&gt;|  County  |  Char  |  10  |  $10.  |  $10.  |  County  | &amp;nbsp; |&lt;BR /&gt;|  County_T  |  Char  |  41  |  &amp;nbsp;  |  &amp;nbsp;  |  County_T  | &amp;nbsp; |&lt;BR /&gt;|  Date  |  Num  |  8  |  DATE9.  |  &amp;nbsp;  |  Date  | &amp;nbsp; |&lt;BR /&gt;|  Date1  |  Num  |  8  |  DATE9.  |  &amp;nbsp;  |  Date1  | &amp;nbsp; |&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Nov 2021 21:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778036#M247629</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2021-11-02T21:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date formatted date9. to create substrings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778059#M247647</link>
      <description>Proc contents reveals that the data variables are numeric of format date9.  So I will apply you other suggestions to move forward.&lt;BR /&gt;&lt;BR /&gt;Thank you.&lt;BR /&gt;&lt;BR /&gt;wlierman</description>
      <pubDate>Tue, 02 Nov 2021 22:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-formatted-date9-to-create-substrings/m-p/778059#M247647</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2021-11-02T22:59:08Z</dc:date>
    </item>
  </channel>
</rss>

