<?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: substr function in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18709#M3724</link>
    <description>It worked fine for me.it is spelled wrong in your code susbtr instead of substr.&lt;BR /&gt;
&lt;BR /&gt;
data one;&lt;BR /&gt;
date='06/01/1989';&lt;BR /&gt;
NewVariable=substr(date,7,4);&lt;BR /&gt;
run;</description>
    <pubDate>Tue, 19 Oct 2010 17:57:30 GMT</pubDate>
    <dc:creator>SASPhile</dc:creator>
    <dc:date>2010-10-19T17:57:30Z</dc:date>
    <item>
      <title>substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18705#M3720</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a date format, e.g. 06/01/1989 and I want a new variable with just the year in it…1989 in this case. However, when I use a substr function, it is returning me odd values.&lt;BR /&gt;
data one;&lt;BR /&gt;
set two;&lt;BR /&gt;
NewVariable=susbtr(date,7,4);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I checked the column description for the date variable and it says &lt;BR /&gt;
TYP:NUM &lt;BR /&gt;
LENGTH: 8&lt;BR /&gt;
FORMAT MMDDYY10.&lt;BR /&gt;
INFORMAT MMDDYY10.&lt;BR /&gt;
&lt;BR /&gt;
 Where am I going wrong?</description>
      <pubDate>Mon, 18 Oct 2010 20:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18705#M3720</guid>
      <dc:creator>spg</dc:creator>
      <dc:date>2010-10-18T20:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18706#M3721</link>
      <description>You should use the year function</description>
      <pubDate>Mon, 18 Oct 2010 20:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18706#M3721</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-10-18T20:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18707#M3722</link>
      <description>that was easy! thanks much melc!</description>
      <pubDate>Mon, 18 Oct 2010 20:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18707#M3722</guid>
      <dc:creator>spg</dc:creator>
      <dc:date>2010-10-18T20:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18708#M3723</link>
      <description>Hi:&lt;BR /&gt;
  The SUBSTR function is meant to work with character strings or character variables. Therefore, using it with a numeric variable would have given you unpredictable results. Numbers are stored in such as way as to make it impossible to go to position 7 or position 4 of the number -- only character strings have fixed positions. So in the variable ALPHA, a character variable of 26 characters:&lt;BR /&gt;
[pre]&lt;BR /&gt;
ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                &lt;BR /&gt;
This code:[pre]&lt;BR /&gt;
newvar = substr(alpha,7,1);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                              &lt;BR /&gt;
would "substring" or extract the 7th character or the G from the alpha variable value.&lt;BR /&gt;
 &lt;BR /&gt;
A SAS numeric variable that represents a date is NOT stored in a "readable" format. For example, the date 01/01/1960 is internally stored as the number 0; while 12/31/1959 is stored as -1 and 01/02/1960 is internally stored as the number 1. This information is explained in the documentation here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm&lt;/A&gt;&lt;BR /&gt;
and&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001397898.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001397898.htm&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 18 Oct 2010 21:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18708#M3723</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-18T21:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18709#M3724</link>
      <description>It worked fine for me.it is spelled wrong in your code susbtr instead of substr.&lt;BR /&gt;
&lt;BR /&gt;
data one;&lt;BR /&gt;
date='06/01/1989';&lt;BR /&gt;
NewVariable=substr(date,7,4);&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 19 Oct 2010 17:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18709#M3724</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-10-19T17:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18710#M3725</link>
      <description>Hi:&lt;BR /&gt;
  If date is a character string (such as you show), then SUBSTR will work. But if date is a numeric value, as shown in the program below, then the results will not be correct. Since the OP said his date value was numeric, SUBSTR on a numeric variable would force a conversion of the internally stored number from numeric to character in order for the SUBSTR to work (since it will ONLY work on CHARACTER variables or text strings).&lt;BR /&gt;
 &lt;BR /&gt;
See the log and program below, where the internally stored date for 06/01/1989 is the number 10744 (Jun 1, 1989 is 10744 days from Jan 1, 1960). So when 10744 was converted to a text string it was converted with the BEST12. format, which resulted in the string xxxxxxx10744 (where every x is a space). So the SUBSTR would get x107 -- where the x is a space -- so the substr results in newvar2 having the string ' 107' with a leading space. (The PUT statement does not show leading spaces -- nor do most SAS procedures.)&lt;BR /&gt;
           &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
1289  data one;&lt;BR /&gt;
1290  chardate='06/01/1989';&lt;BR /&gt;
1291  NewVariable=substr(chardate,7,4);&lt;BR /&gt;
1292  put chardate= newvariable=;&lt;BR /&gt;
1293&lt;BR /&gt;
1294  numdate = '01jun1989'd;&lt;BR /&gt;
1295  newvar2 = substr(numdate,7,4);&lt;BR /&gt;
1296  put "formatted val for " numdate= mmddyy10. " Internal val for " numdate= newvar2=;&lt;BR /&gt;
1297  run;&lt;BR /&gt;
               &lt;BR /&gt;
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).&lt;BR /&gt;
      1295:18&lt;BR /&gt;
chardate=06/01/1989 NewVariable=1989&lt;BR /&gt;
formatted val for numdate=06/01/1989  Internal val for numdate=10744 newvar2=107&lt;BR /&gt;
NOTE: The data set WORK.ONE has 1 observations and 4 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 19 Oct 2010 18:46:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/substr-function/m-p/18710#M3725</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-19T18:46:08Z</dc:date>
    </item>
  </channel>
</rss>

