<?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: Trouble with substr in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593876#M15581</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281255"&gt;@marleeakerson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to extract the year from a date variable (in the dataset as MM/DD/YYYY). But every time I use the substr statement, I get random numbers that are not even in the date variable at all. I first converted the original date variable from the number of days passed from Jan 1,1960 format to the MM/DD/YYYY format so i am wondering if that is contributing to it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
Year=substr(Date,7,4);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You need to show your data.&amp;nbsp; If DATE is a character string like '10/03/2019'&amp;nbsp; then you are asking for characters number 7 thru 10 which would be '2019'.&amp;nbsp; If DATE is number with the MMDDYY10. format attach to it then it will first be converted to a character string using the BEST12. format.&amp;nbsp; So '03OCT2019'd would be converted to:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;"       21825"&lt;/PRE&gt;
&lt;P&gt;And then the 7th to 10th characters would be " 218".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you want a number out?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year=year(date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or a string?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year=put(year(date),4.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Oct 2019 19:42:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-03T19:42:22Z</dc:date>
    <item>
      <title>Trouble with substr</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593867#M15577</link>
      <description>&lt;P&gt;I am trying to extract the year from a date variable (in the dataset as MM/DD/YYYY). But every time I use the substr statement, I get random numbers that are not even in the date variable at all. I first converted the original date variable from the number of days passed from Jan 1,1960 format to the MM/DD/YYYY format so i am wondering if that is contributing to it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
Year=substr(Date,7,4);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 19:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593867#M15577</guid>
      <dc:creator>marleeakerson</dc:creator>
      <dc:date>2019-10-03T19:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with substr</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593872#M15578</link>
      <description>How did you do that conversion? &lt;BR /&gt;Why not just use the YEAR() function on the original variable?&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;year = year(origDate);&lt;BR /&gt;run;</description>
      <pubDate>Thu, 03 Oct 2019 19:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593872#M15578</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-03T19:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with substr</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593873#M15579</link>
      <description>&lt;P&gt;leave the date the way it was if Date is a valid SAS date.&lt;/P&gt;
&lt;P&gt;then do something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
sasdate = "10NOV2009"d;
day = day(sasdate);
month = month(sasdate);
year = year(sasdate);
format sasdate date9.;
run;
  
proc print data = temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 19:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593873#M15579</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-10-03T19:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with substr</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593874#M15580</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281255"&gt;@marleeakerson&lt;/a&gt;&amp;nbsp; First things to note:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Is your data a SAS date numeric variable that is just formatted for display with some format?&lt;/P&gt;
&lt;P&gt;2. If not, is it properly left aligned character variable(though not important, improves sanity checks)&lt;/P&gt;
&lt;P&gt;3. run a proc contents and proc print with obs=10 records to know and see what the data(date variable) really is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above 3 will lead us to next steps&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 19:38:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593874#M15580</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-03T19:38:00Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with substr</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593876#M15581</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281255"&gt;@marleeakerson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to extract the year from a date variable (in the dataset as MM/DD/YYYY). But every time I use the substr statement, I get random numbers that are not even in the date variable at all. I first converted the original date variable from the number of days passed from Jan 1,1960 format to the MM/DD/YYYY format so i am wondering if that is contributing to it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
Year=substr(Date,7,4);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You need to show your data.&amp;nbsp; If DATE is a character string like '10/03/2019'&amp;nbsp; then you are asking for characters number 7 thru 10 which would be '2019'.&amp;nbsp; If DATE is number with the MMDDYY10. format attach to it then it will first be converted to a character string using the BEST12. format.&amp;nbsp; So '03OCT2019'd would be converted to:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;"       21825"&lt;/PRE&gt;
&lt;P&gt;And then the 7th to 10th characters would be " 218".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you want a number out?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year=year(date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or a string?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year=put(year(date),4.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 19:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Trouble-with-substr/m-p/593876#M15581</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-03T19:42:22Z</dc:date>
    </item>
  </channel>
</rss>

