<?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: dynamically assign the greatest number in a variable list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666721#M199538</link>
    <description>&lt;P&gt;The colon wildcard is the best option if the prefixes are suitable.&lt;/P&gt;
&lt;P&gt;Otherwise, if you can create a missing variable after POPnn&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;(let's call it LASTVAR), and if the POP variables are consecutive, you can use&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;sum (of POP1--LASTVAR)&lt;/P&gt;</description>
    <pubDate>Fri, 03 Jul 2020 01:00:25 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-07-03T01:00:25Z</dc:date>
    <item>
      <title>dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666712#M199532</link>
      <description>&lt;P&gt;I am using SAS 9.4 I would like to sum a number of variables, pop1 to popNN, where the greatest number, NN, will differ with every run.&amp;nbsp; Is there a way to reference a number dynamically in a sum(of pop1--popNN) function?&amp;nbsp; Can I use a macro variable here, like sum(of pop1--pop&amp;amp;macrovariable.) ?&lt;/P&gt;&lt;P&gt;thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 21:35:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666712#M199532</guid>
      <dc:creator>GoBlueSAS</dc:creator>
      <dc:date>2020-07-02T21:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666713#M199533</link>
      <description>&lt;P&gt;You can do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   sum_of_pop=sum(of pop:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which tells SAS to sum all variables whose name begins with the letters "POP".&amp;nbsp; SAS will figure out how many such variables there are.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 21:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666713#M199533</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-02T21:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666714#M199534</link>
      <description>&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use colon wildcard option here, assuming you have a unique and consistent prefix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(of pop:)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/336317"&gt;@GoBlueSAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am using SAS 9.4 I would like to sum a number of variables, pop1 to popNN, where the greatest number, NN, will differ with every run.&amp;nbsp; Is there a way to reference a number dynamically in a sum(of pop1--popNN) function?&amp;nbsp; Can I use a macro variable here, like sum(of pop1--pop&amp;amp;macrovariable.) ?&lt;/P&gt;
&lt;P&gt;thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 21:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666714#M199534</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-02T21:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666721#M199538</link>
      <description>&lt;P&gt;The colon wildcard is the best option if the prefixes are suitable.&lt;/P&gt;
&lt;P&gt;Otherwise, if you can create a missing variable after POPnn&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;(let's call it LASTVAR), and if the POP variables are consecutive, you can use&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;sum (of POP1--LASTVAR)&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 01:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666721#M199538</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-03T01:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666744#M199550</link>
      <description>&lt;P&gt;You should also consider working with a long dataset layout, where you have only one variable and sum vertically as needed.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 07:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666744#M199550</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-03T07:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666765#M199562</link>
      <description>&lt;P&gt;Yet another take on this.&lt;/P&gt;
&lt;P&gt;The colon wildcard ,sum(of pop:), is the simplest, but it may not work if you have other variables that begin with POP.&lt;/P&gt;
&lt;P&gt;The double dash approach ,sum(of pop--lastvar) only works if the POP variables are last in your input data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A more safe approach may be this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  retain pop1-pop99 .;
  set have;
  sum=sum(of pop1-pop99);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way, all the variables will exist, and the order of the variables on the dataset or the names of other variables do not matter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: changed the retain value from 0 to missing, so that means or standard deviations will also be calculated correctly.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 08:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666765#M199562</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-03T08:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically assign the greatest number in a variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666922#M199634</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yet another take on this.&lt;/P&gt;
&lt;P&gt;The colon wildcard ,sum(of pop:), is the simplest, but it may not work if you have other variables that begin with POP.&lt;/P&gt;
&lt;P&gt;The double dash approach ,sum(of pop--lastvar) only works if the POP variables are last in your input data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A more safe approach may be this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  retain pop1-pop99 .;
  set have;
  sum=sum(of pop1-pop99);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way, all the variables will exist, and the order of the variables on the dataset or the names of other variables do not matter.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is a good warning and a good solution, but if the user only started out with say POP1-POP59, they would now have 40 excess variables in the output dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One might get around this by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  sum=sum(of pop1:, of pop2:, of pop3:, of pop4:, of pop5:, of pop6:, of pop7:, of pop8:, of pop9:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's a bit wordy but:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It wouldn't include other variables with names like POP_US, POP_WORLD, etc.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;It would sum up all the variables of interest, and&lt;/LI&gt;
&lt;LI&gt;it wouldn't inadvertently create any extra variables in the output dataset.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jul 2020 03:37:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-assign-the-greatest-number-in-a-variable-list/m-p/666922#M199634</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-04T03:37:34Z</dc:date>
    </item>
  </channel>
</rss>

