<?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: Creating a new variable from a existing variable value in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690307#M24712</link>
    <description>&lt;P&gt;This is where the "automatic remerge" of SAS SQL comes in handy:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input serviceitemcd $ Count;
datalines;
AB    10
AC    20
AD    30
;

proc sql;
create table want as
  select
    serviceitemcd,
    count,
    sum(count) as grandtotal
  from have
  union all
  select
    "Total" as serviceitemcd,
    sum(count) as count,
    sum(count) as grandtotal
  from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Oct 2020 10:55:25 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-10-09T10:55:25Z</dc:date>
    <item>
      <title>Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690300#M24711</link>
      <description>&lt;DIV&gt;Hi, this should be simple but I am struggling.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;I have a dataset with 2 variables - service item and a count. Included in the service item variable I have also created a record called Total (which is the sum of all service items) . What I want to do, is create a 3rd variable on every record that&amp;nbsp;uses thetotal value I created.&amp;nbsp; So, what I want is&amp;nbsp;3 variables and&amp;nbsp; every record will have&amp;nbsp; Service item, count, and&amp;nbsp; Grand total (as a new variable).&lt;/DIV&gt;&lt;DIV&gt;ie&lt;/DIV&gt;&lt;DIV&gt;serviceitemcd &amp;nbsp;&amp;nbsp; Count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GrandTotal&lt;/DIV&gt;&lt;DIV&gt;AB&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; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;60&lt;/DIV&gt;&lt;DIV&gt;AC&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; 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;60&lt;/DIV&gt;&lt;DIV&gt;AD&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; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 60&lt;/DIV&gt;&lt;DIV&gt;Total&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;60&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 60&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have tried doing a data merge where the total is in one data set and the service items and count is in another dataset. But when I do this, it creates a value for the Total Grand Total record but the other service items the Grand total is missing.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This is my code. Thanks for your help..&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;proc summary data = physio_recd nway;&lt;BR /&gt;class serviceitemcd;&lt;BR /&gt;var INVOICE_AM;&lt;BR /&gt;output out=dat.physio_item (drop=_type_ _freq_) n=count;&lt;BR /&gt;&lt;BR /&gt;proc summary data = physio_recd nway;&lt;BR /&gt;class;&lt;BR /&gt;var INVOICE_AM;&lt;BR /&gt;output out=physio_tot (drop=_type_ _freq_) n=GrandTotal;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;data physio_tot;&lt;BR /&gt;set physio_tot;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;SERVICE_ITEM_CD = 'ZZTotal';&lt;BR /&gt;&amp;nbsp;count = GrandTotal;;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;BR /&gt;proc sort data = dat.physio_item;&lt;BR /&gt;by SERVICE_ITEM_CD;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;proc sort data = physio_tot;&lt;BR /&gt;by SERVICE_ITEM_CD;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;BR /&gt;data dat.physio_item;&lt;BR /&gt;merge dat.physio_item&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; physio_tot;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Oct 2020 09:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690300#M24711</guid>
      <dc:creator>Collingwoodeyed</dc:creator>
      <dc:date>2020-10-09T09:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690307#M24712</link>
      <description>&lt;P&gt;This is where the "automatic remerge" of SAS SQL comes in handy:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input serviceitemcd $ Count;
datalines;
AB    10
AC    20
AD    30
;

proc sql;
create table want as
  select
    serviceitemcd,
    count,
    sum(count) as grandtotal
  from have
  union all
  select
    "Total" as serviceitemcd,
    sum(count) as count,
    sum(count) as grandtotal
  from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 10:55:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690307#M24712</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-09T10:55:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690314#M24713</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/351604"&gt;@Collingwoodeyed&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;has demonstrated, a single PROC SQL step can streamline the process and replace several PROC SUMMARY and DATA steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to provide a fix to your last DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then set physio_tot;
set dat.physio_item physio_tot(drop=GrandTotal);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The purpose of the first SET statement is basically to read the single value of&amp;nbsp;&lt;FONT face="courier new,courier"&gt;GrandTotal&lt;/FONT&gt;, which is then&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=en#p0wmd1lm606r67n1m6somosa8p0y" target="_blank" rel="noopener"&gt;automatically retained&lt;/A&gt; through all iterations of the DATA step. It also reads &lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;serviceitemcd&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;Count&lt;/FONT&gt;&amp;nbsp;&lt;/SPAN&gt;(which is not bad because it helps to produce the desired variable order) from &lt;FONT face="courier new,courier"&gt;physio_tot&lt;/FONT&gt;, but their values are overwritten by the second SET statement. Without the DROP= dataset option also the &lt;FONT face="courier new,courier"&gt;GrandTotal&lt;/FONT&gt; would be overwritten with missing values for the observations from &lt;FONT face="courier new,courier"&gt;dat.physio_item&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 11:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690314#M24713</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-09T11:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690350#M24714</link>
      <description>&lt;P&gt;Hi Kurl, thanks for answering my post and reply so quick. Your solution are exactly what was after and your explanation also helped. Kind Regards&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 12:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690350#M24714</guid>
      <dc:creator>Collingwoodeyed</dc:creator>
      <dc:date>2020-10-09T12:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690372#M24715</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/351604"&gt;@Collingwoodeyed&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;Glad to see that KurtBremser's solution worked for you. Then it would be fair and help later readers if you marked his helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select his post&amp;nbsp;as the solution after clicking&amp;nbsp;"Not the Solution" in the option menu (see icon below) of the current solution.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="show_option_menu.png" style="width: 133px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50448iF973B9932D4CB064/image-size/large?v=v2&amp;amp;px=999" role="button" title="show_option_menu.png" alt="show_option_menu.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 13:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690372#M24715</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-09T13:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690376#M24716</link>
      <description>&lt;P&gt;Sorry if I did something wrong. I just wanted to personally acknowledge Kurl for answering my question. I did mark this as solved, so unsure why you are querying this (I didn't do it straight away, as didn't realise that this needed to be done. If I have missed something, then I am sorry&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 13:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690376#M24716</guid>
      <dc:creator>Collingwoodeyed</dc:creator>
      <dc:date>2020-10-09T13:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690399#M24717</link>
      <description>&lt;P&gt;Thank-you also for your answer and going to trouble to explain an alternative solution.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 14:12:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690399#M24717</guid>
      <dc:creator>Collingwoodeyed</dc:creator>
      <dc:date>2020-10-09T14:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from a existing variable value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690416#M24718</link>
      <description>&lt;P&gt;You're welcome. It's great to write a "thank you" post, but the post &lt;EM&gt;marked as the solution&lt;/EM&gt; (with the green background color) should ideally be the one which contains the actual solution (KurtBremser's post in this case), so that later readers with a similar problem can quickly find the helpful SAS code (rather than a post only referring to it). Moreover, there are rankings based on the number of provided solutions (&lt;A href="https://communities.sas.com/t5/solutions/acceptedsolutionsleaderboardpage/node-display-id/community%3Akntur85557/timerange/all" target="_blank" rel="noopener"&gt;Top Solution Authors Leaderboard&lt;/A&gt;) and obviously it's KurtBremser who deserves the point here, but he won't get it until his post is marked as the solution.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 14:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-variable-from-a-existing-variable-value/m-p/690416#M24718</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-09T14:27:24Z</dc:date>
    </item>
  </channel>
</rss>

