<?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: 2 questions: How to use substr in PROC SQL and how to modify a variable in an indexed SAS datase in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895145#M39801</link>
    <description>&lt;P&gt;To update data in place look at the MODIFY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make a NEW dataset with the same index(es) then just specify the index(es) in the DATA statement.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Sep 2023 21:36:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-09-20T21:36:31Z</dc:date>
    <item>
      <title>2 questions: How to use substr in PROC SQL and how to modify a variable in an indexed SAS dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895085#M39798</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I have a dataset with a CHAR variable named YR that has values in YYYYMM, like 202101 to 202112 and I wanted to modify this dataset so that the YYYY will be 2023, 202301 to 202312. Also, this dataset is indexed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used the following code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
  update mydata
  set substr(YR, 1, 4) = '2023';
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For some reason, I keep getting an error of Expecting =.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Question 1:&lt;/P&gt;&lt;P&gt;What's the proper way to use the substr function in PROC SQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Question 2:&lt;/P&gt;&lt;P&gt;What is the correct method for modify this variable in a DATA step without losing the index?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 15:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895085#M39798</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2023-09-20T15:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: 2 questions: How to use substr in PROC SQL and how to modify a variable in an indexed SAS datase</title>
      <link>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895096#M39799</link>
      <description>&lt;P&gt;One example with actual data values&lt;/P&gt;
&lt;PRE&gt;data have;
  input YR $;
datalines;
199901
199902
202109
;


proc sql;
   update have
   set yr= cats('2023',substr(yr,5));
quit;
 &lt;/PRE&gt;
&lt;P&gt;Why: From documentation from Proc SQL&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-eDocBody"&gt;
&lt;DIV class="xis-refProc"&gt;
&lt;DIV class="xis-procStatement"&gt;
&lt;DIV class="xis-procStatementSyntax"&gt;
&lt;H2 id="p0sd4rrefjl7y7n16wkhjjqbcefi" class="xis-title"&gt;Syntax&lt;/H2&gt;
&lt;DIV class="xis-syntaxSimple"&gt;
&lt;DIV class="xis-syntaxLevel"&gt;&lt;SPAN class="xis-keyword"&gt;&lt;FONT style="background-color: #fcdec0;"&gt;UPDATE&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="xis-argRequired"&gt;&lt;SPAN class="xis-choice"&gt;&lt;A title="Description of syntax: table-name" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p1l2essyb37amxn1r9vg0zdq7smr" target="_blank"&gt;&lt;SPAN class="xis-userSuppliedValue"&gt;table-name&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN class="xis-choice"&gt; | &lt;A title="Description of syntax: sas/access-view" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0k24a12usjs2pn1u6luvs93j7vx" target="_blank"&gt;&lt;SPAN class="xis-userSuppliedValue"&gt;sas/access-view&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN class="xis-choice"&gt; | &lt;A title="Description of syntax: proc-sql-view" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#n0f00hwc6ppuayn12fa9dht3chq8" target="_blank"&gt;&lt;SPAN class="xis-userSuppliedValue"&gt;proc-sql-view&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="xis-argOptional"&gt;&amp;lt;&lt;SPAN class="xis-keyword"&gt;AS&lt;/SPAN&gt; &lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: alias" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p1a1j2bmerebvcn18r9y3nenhzxn" target="_blank"&gt;alias&lt;/A&gt;&lt;/SPAN&gt;&amp;gt;&lt;/SPAN&gt;
&lt;DIV class="xis-syntaxLevel"&gt;&lt;SPAN class="xis-keyword"&gt;SET&lt;/SPAN&gt; &lt;SPAN class="xis-argRequired"&gt;&lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: column" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0c4o1ss4qx67un1fl3va232edoi" target="_blank"&gt;column-1&lt;/A&gt;&lt;/SPAN&gt;=&lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: sql-expression" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0o2a4um8rzipdn13sxtfiiq4yly" target="_blank"&gt;sql-expression-1&lt;/A&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="xis-argOptional"&gt;&amp;lt;, &lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: column" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0c4o1ss4qx67un1fl3va232edoi" target="_blank"&gt;column-2&lt;/A&gt;&lt;/SPAN&gt;=&lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: sql-expression" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0o2a4um8rzipdn13sxtfiiq4yly" target="_blank"&gt;sql-expression-2&lt;/A&gt;&lt;/SPAN&gt;, &lt;SPAN class="xis-symbolHEllipsis"&gt;...&lt;/SPAN&gt;&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="xis-argOptional"&gt;&amp;lt;&lt;SPAN class="xis-keyword"&gt;SET&lt;/SPAN&gt; &lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: column" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0c4o1ss4qx67un1fl3va232edoi" target="_blank"&gt;column-1&lt;/A&gt;&lt;/SPAN&gt;=&lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: sql-expression" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0o2a4um8rzipdn13sxtfiiq4yly" target="_blank"&gt;sql-expression-1&lt;/A&gt;&lt;/SPAN&gt; &amp;lt;, &lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: column" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0c4o1ss4qx67un1fl3va232edoi" target="_blank"&gt;column-1&lt;/A&gt;&lt;/SPAN&gt;=&lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: sql-expression" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0o2a4um8rzipdn13sxtfiiq4yly" target="_blank"&gt;sql-expression-2&lt;/A&gt;&lt;/SPAN&gt;, &lt;SPAN class="xis-symbolHEllipsis"&gt;...&lt;/SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="xis-syntaxLevel"&gt;&lt;SPAN class="xis-argOptional"&gt;&amp;lt;&lt;SPAN class="xis-keyword"&gt;WHERE&lt;/SPAN&gt; &lt;SPAN class="xis-userSuppliedSyntaxValue"&gt;&lt;A title="Description of syntax: sql-expression" href="http://127.0.0.1:53501/help/sqlproc.hlp/p0ci36zwxhm1xdn1a943yeczfalk.htm#p0o2a4um8rzipdn13sxtfiiq4yly" target="_blank"&gt;sql-expression&lt;/A&gt;&lt;/SPAN&gt;&amp;gt;&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Set COLUMN=&amp;nbsp;&amp;nbsp; not Set &amp;lt;some function call&amp;gt;= . You need to use the variable name as the target of the SET.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for changing the value of a variable without changing the index think of what an INDEX is: as short cut for finding specific values. If you want your data set to be indexed in file order add another variable and make that the index. If you want to use this variable then reindex on the new values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now for the maybe more important part: WHY is a date value in a character variable? You lose all of the date functions and have to do gyrations with odd code like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 16:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895096#M39799</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-20T16:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: 2 questions: How to use substr in PROC SQL and how to modify a variable in an indexed SAS datase</title>
      <link>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895100#M39800</link>
      <description>Hi ballardw, thanks for the explanation!&lt;BR /&gt;&lt;BR /&gt;Regarding the index question, I was asking if there's a way using DATA step to modify a variable's value without LOSING the index associated with that dataset. I understand that when updating a dataset, the associated index file will also be updated. I tried the following code it'll delete the index file afterwards:&lt;BR /&gt;data have;&lt;BR /&gt;set have;&lt;BR /&gt;substr(YR,1,4) = '2023';&lt;BR /&gt;run;&lt;BR /&gt;Assuming that the dataset HAVE is indexed.&lt;BR /&gt;&lt;BR /&gt;For the question regarding why the YR variable is in CHAR form and not in NUM, well, I didn't create the original dataset and just need to work on it as is. I totally agree with you that it shouldn't be in CHAR.</description>
      <pubDate>Wed, 20 Sep 2023 17:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895100#M39800</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2023-09-20T17:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: 2 questions: How to use substr in PROC SQL and how to modify a variable in an indexed SAS datase</title>
      <link>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895145#M39801</link>
      <description>&lt;P&gt;To update data in place look at the MODIFY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make a NEW dataset with the same index(es) then just specify the index(es) in the DATA statement.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2023 21:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/2-questions-How-to-use-substr-in-PROC-SQL-and-how-to-modify-a/m-p/895145#M39801</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-20T21:36:31Z</dc:date>
    </item>
  </channel>
</rss>

