<?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: Getting total of variable to use in new variable calculation in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739312#M28951</link>
    <description>&lt;P&gt;Your merge is not correct. Please review the example to see my use of the double SET statement instead.&lt;/P&gt;
&lt;P&gt;FYI - The NOPRINT option is specified on the PROC MEANS which suppresses any results. If you'd like to see them, remove the NOPRINT option.&lt;/P&gt;</description>
    <pubDate>Wed, 05 May 2021 18:59:32 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-05-05T18:59:32Z</dc:date>
    <item>
      <title>Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739266#M28941</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using a dataset with 12 numeric variables. I need to use a total of one variable, x12, to calculate every observation's percentage of the whole, and I would like to do this within the DATA step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA example;
INPUT x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12;
x13 = (x12 / total of x12) * 100;

DATALINES;
... ... ... ... ... ... .. .. .. 200
... ... .. .. .. .. .. ..  .. .. 300

;
PROC PRINT DATA = example;
                        SUM(x12)
                        SUM(x13)
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, I need to obtain the sum of all x12 values and use it in the calculation of a new variable, x13 which is a relative percentage. The formula to calculate x13 is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;x13 = (current x12 value / total sum of all x12 values) * 100&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with obtaining the total sum of all x12 values in the DATA step?&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 16:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739266#M28941</guid>
      <dc:creator>JethrowCatch</dc:creator>
      <dc:date>2021-05-05T16:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739268#M28942</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA example;
INPUT x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12;
array x x1-x12;
array pct pct1-pct12;
rowsum=sum(of x1-x12);
do i=1 to 12;
    pct(i) = (x(i) / rowsum) * 100;
end;
datalines;

;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 May 2021 16:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739268#M28942</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-05T16:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739277#M28944</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your swift response. I think you misunderstood my question. I need to obtain the sum of every value in the column x12, not each row.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 17:01:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739277#M28944</guid>
      <dc:creator>JethrowCatch</dc:creator>
      <dc:date>2021-05-05T17:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739279#M28945</link>
      <description>&lt;P&gt;SQL is faster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, sum(x12) as total,  x12 / calculated total as x13
from example;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise, you need to:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a total variable&lt;/LI&gt;
&lt;LI&gt;Merge it in to your data set&lt;/LI&gt;
&lt;LI&gt;Do the calculation.&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If you really want a data step, see this example on how to merge in averages (SUM) would be the total and you can modify it as needed.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&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/380938"&gt;@JethrowCatch&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using a dataset with 12 numeric variables. I need to use a total of one variable, x12, to calculate every observation's percentage of the whole, and I would like to do this within the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA example;
INPUT x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12;
x13 = (x12 / total of x12) * 100;

DATALINES;
... ... ... ... ... ... .. .. .. 200
... ... .. .. .. .. .. ..  .. .. 300

;
PROC PRINT DATA = example;
                        SUM(x12)
                        SUM(x13)
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, I need to obtain the sum of all x12 values and use it in the calculation of a new variable, x13 which is a relative percentage. The formula to calculate x13 is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x13 = (current x12 value / total sum of all x12 values) * 100&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me with obtaining the total sum of all x12 values in the DATA step?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 17:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739279#M28945</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-05T17:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739285#M28946</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your response. We have not learned about PROC SQL in school yet. I do not want to seem as if I am cheating by using functions I haven't learned yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have given your 3 step method a go, but I am having some trouble understanding how to obtain the total value before calculating the percentage.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA example;
INPUT x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12;
RETAIN TOTAL 0;
TOTAL = sum(TOTAL, x12);

DATALINES;
.....
;

DATA PCT;  			
MERGE  example; BY x1;
x13 =  (x12/TOTAL) * 100;
RUN;	

PROC PRINT DATA = PCT;
RUN:&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My problem is that the value of TOTAL increases for each observation. I need for TOTAL to be a variable that has one value, which is the value it takes on after adding the x12 value of the last observation to itself.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 17:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739285#M28946</guid>
      <dc:creator>JethrowCatch</dc:creator>
      <dc:date>2021-05-05T17:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739291#M28947</link>
      <description>&lt;P&gt;In other words, what I would like to do is to use a specific index of TOTAL in every x13 calculation. However, indexing with curly or straight brackets does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, the x13 calculation should look as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;x13 = ( x12 / TOTAL[30]) * 100;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;30 is my number of observations in the example dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would anyone happen to know how to index observations in SAS?&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 17:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739291#M28947</guid>
      <dc:creator>JethrowCatch</dc:creator>
      <dc:date>2021-05-05T17:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739293#M28948</link>
      <description>Did you work through the example at the link?&lt;BR /&gt;Note the PROC MEANS step that calculated the average, you would modify that to calculate the sum.&lt;BR /&gt;</description>
      <pubDate>Wed, 05 May 2021 17:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739293#M28948</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-05T17:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739302#M28949</link>
      <description>&lt;P&gt;Yes, I have. I now obtain the total that I need in the variable TOTALCT. However, the output that I get under the OUTPUT DATA tab is a table showing type, frequency, and total.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output that I need is a table showing all the original variables x1-x12, with x13 added on. Therefore, I need to use PROC PRINT, but at this moment, nothing shows up under the results tab.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC MEANS DATA = EXAMPLE NOPRINT;
    OUTPUT OUT = example SUM(x12) = TOTALCT;
run;
	  
DATA PCT;
MERGE example; BY x1;
x13 = (x12 / TOTALCT) * 100;
RUN;

PROC PRINT DATA = PCT;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 May 2021 18:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739302#M28949</guid>
      <dc:creator>JethrowCatch</dc:creator>
      <dc:date>2021-05-05T18:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Getting total of variable to use in new variable calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739312#M28951</link>
      <description>&lt;P&gt;Your merge is not correct. Please review the example to see my use of the double SET statement instead.&lt;/P&gt;
&lt;P&gt;FYI - The NOPRINT option is specified on the PROC MEANS which suppresses any results. If you'd like to see them, remove the NOPRINT option.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 18:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Getting-total-of-variable-to-use-in-new-variable-calculation/m-p/739312#M28951</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-05T18:59:32Z</dc:date>
    </item>
  </channel>
</rss>

