<?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: Sum 2 columns based on format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784177#M250201</link>
    <description>&lt;P&gt;Formats are used to control how values are display &lt;STRONG&gt;as text&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use them with the PUT() function to help you with transforming values, but if you want the result to be a number you will need to add an INPUT() function call to convert the generated text into a number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input A B ;
cards;
3    4
5    5
3    4
;

PROC FORMAT;
value test
 3 = 0.5
 4 = 0.7
 5 = 1
;
run;

data want;
  set have;
  array old A B ;
  array new new_A new_B ;
  do index=1 to dim(old);
    new[index] = input(put(old[index],test.),32.);
  end;
  sum=sum(of old[*]);
  new_sum=sum(of new[*]);
  drop index;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    A    B    new_A    new_B    sum    new_sum

 1     3    4     0.5      0.7       7      1.2
 2     5    5     1.0      1.0      10      2.0
 3     3    4     0.5      0.7       7      1.2
&lt;/PRE&gt;</description>
    <pubDate>Sun, 05 Dec 2021 14:07:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-12-05T14:07:53Z</dc:date>
    <item>
      <title>Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784173#M250198</link>
      <description>&lt;P&gt;I am trying to create a new variable that will represent the sum of 2 other columns for each row.&lt;/P&gt;&lt;P&gt;The issue i am having is the following. My original data looks like this:&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; sum&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I must change the values of A and B based on certain criteria and afterwards calculate the sum. So for example after the FORMAT i get this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC FORMAT;
value test
3 = 0.5
4 = 0.7
5 = 1
;
run;

data have_f;
set have;
Format A B test.;
run;

data want;
set have_f;
sum = sum(of A B);
run;&lt;/PRE&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp;sum&lt;/P&gt;&lt;P&gt;0.5&amp;nbsp; 0.7&amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;0.5&amp;nbsp; 0.7&amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i calculate the sum of the formatted values instead of the original ones?&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 13:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784173#M250198</guid>
      <dc:creator>Traian</dc:creator>
      <dc:date>2021-12-05T13:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784175#M250199</link>
      <description>&lt;P&gt;How many values does the "format" need to accommodate? Is it just 3,4,5, or are there more?&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 13:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784175#M250199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-05T13:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784176#M250200</link>
      <description>&lt;P&gt;The complete format also has 1 and 2 but both of those are turned into 0. I have to apply this format to around 20 variables and eventually calculate multiple sums between the formatted 2 to 6 variables.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 13:58:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784176#M250200</guid>
      <dc:creator>Traian</dc:creator>
      <dc:date>2021-12-05T13:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784177#M250201</link>
      <description>&lt;P&gt;Formats are used to control how values are display &lt;STRONG&gt;as text&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use them with the PUT() function to help you with transforming values, but if you want the result to be a number you will need to add an INPUT() function call to convert the generated text into a number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input A B ;
cards;
3    4
5    5
3    4
;

PROC FORMAT;
value test
 3 = 0.5
 4 = 0.7
 5 = 1
;
run;

data want;
  set have;
  array old A B ;
  array new new_A new_B ;
  do index=1 to dim(old);
    new[index] = input(put(old[index],test.),32.);
  end;
  sum=sum(of old[*]);
  new_sum=sum(of new[*]);
  drop index;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    A    B    new_A    new_B    sum    new_sum

 1     3    4     0.5      0.7       7      1.2
 2     5    5     1.0      1.0      10      2.0
 3     3    4     0.5      0.7       7      1.2
&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Dec 2021 14:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784177#M250201</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-05T14:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784178#M250202</link>
      <description>I see, i completely missunderstood what formats did. I used one to convert some of my data into probability and apply pca to it and now realized that it used the original values to establish the principal components. If this is the case, is there an easy way to just completely modify the data based on that format? As inthis set:&lt;BR /&gt;A B&lt;BR /&gt;5 5&lt;BR /&gt;4 5&lt;BR /&gt;3 5&lt;BR /&gt;&lt;BR /&gt;becomes this set:&lt;BR /&gt;A B&lt;BR /&gt;1 1&lt;BR /&gt;0.7 1&lt;BR /&gt;0.5 1&lt;BR /&gt;Don't need to keep the original values in this case.</description>
      <pubDate>Sun, 05 Dec 2021 14:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784178#M250202</guid>
      <dc:creator>Traian</dc:creator>
      <dc:date>2021-12-05T14:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784183#M250203</link>
      <description>&lt;P&gt;Just write the result back into the same variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a=input(put(a,test.),32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure to not overwrite your source dataset if you do this.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 15:09:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784183#M250203</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-05T15:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784184#M250204</link>
      <description>&lt;P&gt;Note that if you are reading the original data from a text file you could define an INFORMAT instead of FORMAT.&amp;nbsp; You use an INFORMAT to convert text into values.&amp;nbsp; You use the INVALUE statement to define an INFORMAT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC FORMAT;
invalue test
 '3' = 0.5
 '4' = 0.7
 '5' = 1
;
run;


data have;
  input (A B)(:test.);
cards;
3    4
5    5
3    4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs     A      B

 1     0.5    0.7
 2     1.0    1.0
 3     0.5    0.7

&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Dec 2021 15:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784184#M250204</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-05T15:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784186#M250205</link>
      <description>&lt;P&gt;I see, i tried adapting your answer to my dataset..&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
  set Alcool_A_Q3;
  array prob a1 a4 a6 a7 a8;
  do index=1 to dim(prob);
    prob[index] = input(put(prob[index],los.),32.);
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried it out on 5 variables and it works fine. I wanted to know if there was a way to pass the variables to the array by their index in the dataset so i avoid typing them all( if there is an option like that to begin with). The variable names become very complex at some point so it'll be hard to make sure i didn't make any typos. Is there a simple way to pass columns from 3rd to 23rd to the array? Also thank you for the answers, it helped me tremenduously.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 15:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784186#M250205</guid>
      <dc:creator>Traian</dc:creator>
      <dc:date>2021-12-05T15:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sum 2 columns based on format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784198#M250211</link>
      <description>&lt;P&gt;You can use a number of different variable lists.&lt;/P&gt;
&lt;P&gt;_all_ means ALL of the variables, probably not useful for this as it might include character variables.&lt;/P&gt;
&lt;P&gt;_numeric_ means all of the numeric variables.&lt;/P&gt;
&lt;P&gt;_character_ means all of the character variables.&lt;/P&gt;
&lt;P&gt;If you name the variables with sequential numeric suffix you can use a simple variable list:&amp;nbsp; a1-a8&lt;/P&gt;
&lt;P&gt;If you know the first and last variable name in your list you can use positional variable list:&amp;nbsp; a1--a8&lt;/P&gt;
&lt;P&gt;You can even pick only the numeric variables in a positional range:&amp;nbsp; a1-numeric-a8&lt;/P&gt;
&lt;P&gt;You can also pick all variables whose&amp;nbsp; name starts with a common prefix by using a colon.&amp;nbsp; a:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 18:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-2-columns-based-on-format/m-p/784198#M250211</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-05T18:21:12Z</dc:date>
    </item>
  </channel>
</rss>

