<?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 Apply substring Function to all variables in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468111#M285404</link>
    <description>&lt;P&gt;I'm looking to apply a substring function to all columns in a dataset. Currently I've only had success doing it to one column at a time, but this isn't efficient or viable as this is a macro that won't always know the names or&amp;nbsp;the amount of columns(There's also 100's of columns)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; VarN&lt;/P&gt;&lt;P&gt;01-AB&amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/P&gt;&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14-EF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;99-XX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08-BC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;... VarN&lt;/P&gt;&lt;P&gt;01&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99&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; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;99&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 99&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I said I can achieve the desired result using the below code, but I have to specify the column names. Really, I need the following function to apply to ALL columns, without specifying their names. I ran into the same problem using the compress function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Have;&lt;BR /&gt;set Want;&lt;BR /&gt;length var1 4;&lt;BR /&gt;&lt;SPAN&gt;var1&amp;nbsp;&lt;/SPAN&gt; = substr(var1, 1, index(var1, '-') - 1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance&lt;/P&gt;</description>
    <pubDate>Wed, 06 Jun 2018 15:54:16 GMT</pubDate>
    <dc:creator>OPrxwr7</dc:creator>
    <dc:date>2018-06-06T15:54:16Z</dc:date>
    <item>
      <title>Apply substring Function to all variables in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468111#M285404</link>
      <description>&lt;P&gt;I'm looking to apply a substring function to all columns in a dataset. Currently I've only had success doing it to one column at a time, but this isn't efficient or viable as this is a macro that won't always know the names or&amp;nbsp;the amount of columns(There's also 100's of columns)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; VarN&lt;/P&gt;&lt;P&gt;01-AB&amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/P&gt;&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14-EF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;99-XX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08-BC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;... VarN&lt;/P&gt;&lt;P&gt;01&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99&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; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;99&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 99&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I said I can achieve the desired result using the below code, but I have to specify the column names. Really, I need the following function to apply to ALL columns, without specifying their names. I ran into the same problem using the compress function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Have;&lt;BR /&gt;set Want;&lt;BR /&gt;length var1 4;&lt;BR /&gt;&lt;SPAN&gt;var1&amp;nbsp;&lt;/SPAN&gt; = substr(var1, 1, index(var1, '-') - 1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 15:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468111#M285404</guid>
      <dc:creator>OPrxwr7</dc:creator>
      <dc:date>2018-06-06T15:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: Apply substring Function to all variables in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468115#M285405</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (Var1      Var2         Var3        Var4) ($);
cards;
01-AB    NA            NA             99-XX
NA         NA            14-EF        99-XX
NA         08-BC       NA             99-XX
;

data want;
set have;
array t(*) var:;
do _n_=1 to dim(t);
if index(t(_n_), '-')&amp;gt;0 then t(_n_)=substr(t(_n_), 1, index(t(_n_), '-') - 1);
else call missing(t(_n_));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jun 2018 16:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468115#M285405</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-06T16:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Apply substring Function to all variables in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468117#M285406</link>
      <description>&lt;P&gt;Are your variables actually named Var1-VarN? Do they have a specific naming convention? Do you have all character variables or mixed types?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use _character_ and _numeric_ to list all character and numeric variables respectively.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/213987"&gt;@OPrxwr7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm looking to apply a substring function to all columns in a dataset. Currently I've only had success doing it to one column at a time, but this isn't efficient or viable as this is a macro that won't always know the names or&amp;nbsp;the amount of columns(There's also 100's of columns)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; VarN&lt;/P&gt;
&lt;P&gt;01-AB&amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/P&gt;
&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14-EF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;99-XX&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08-BC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99-XX&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want:&lt;BR /&gt;Var1&amp;nbsp; &amp;nbsp; &amp;nbsp; Var2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;... VarN&lt;/P&gt;
&lt;P&gt;01&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;99&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; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;99&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;08&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 99&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said I can achieve the desired result using the below code, but I have to specify the column names. Really, I need the following function to apply to ALL columns, without specifying their names. I ran into the same problem using the compress function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data Have;&lt;BR /&gt;set Want;&lt;BR /&gt;length var1 4;&lt;BR /&gt;&lt;SPAN&gt;var1&amp;nbsp;&lt;/SPAN&gt; = substr(var1, 1, index(var1, '-') - 1);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in Advance&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 16:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468117#M285406</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-06T16:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Apply substring Function to all variables in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468119#M285407</link>
      <description>&lt;P&gt;Fantastic! Thank you this is perfect!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 16:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468119#M285407</guid>
      <dc:creator>OPrxwr7</dc:creator>
      <dc:date>2018-06-06T16:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: Apply substring Function to all variables in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468120#M285408</link>
      <description>&lt;P&gt;Sorry I should have mentioned, yes all the variable names will have a consistent "var" at the beginning, so novinosrin's use of the wildcards will work. Thanks for looking into it though!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 16:19:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-substring-Function-to-all-variables-in-a-dataset/m-p/468120#M285408</guid>
      <dc:creator>OPrxwr7</dc:creator>
      <dc:date>2018-06-06T16:19:32Z</dc:date>
    </item>
  </channel>
</rss>

