<?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: Populating a new column in an existing dataset in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233501#M2465</link>
    <description>&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller﻿&lt;/a&gt;. SAS stores data sets rowwise, so to add a column you actually need to create a new data set.&amp;nbsp; You can append &lt;EM&gt;rows&lt;/EM&gt; to an existing data set, but not columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do this operation entirely in IML, but it will be more efficient to use the MERGE statement in the DATA step to combine the data (unless all the data are already in IML vectors or matrices).&lt;/P&gt;</description>
    <pubDate>Fri, 06 Nov 2015 17:45:34 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2015-11-06T17:45:34Z</dc:date>
    <item>
      <title>Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233493#M2463</link>
      <description>&lt;P&gt;I need to write the contents of a 1-column matrix into a new column in an existing dataset. Is there a way of doing this in IML such that the values being written into the new column line up with the corresponding content in the old data? (The matrix and the old dataset have the same number of rows).&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 17:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233493#M2463</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T17:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233497#M2464</link>
      <description>&lt;P&gt;You write the contents of the 1-column matrix to a SAS data set (IML commands to use are CREATE and APPEND). Then in a SAS data step, you merge the original data set and the data set created from PROC IML.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 17:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233497#M2464</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-11-06T17:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233501#M2465</link>
      <description>&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller﻿&lt;/a&gt;. SAS stores data sets rowwise, so to add a column you actually need to create a new data set.&amp;nbsp; You can append &lt;EM&gt;rows&lt;/EM&gt; to an existing data set, but not columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do this operation entirely in IML, but it will be more efficient to use the MERGE statement in the DATA step to combine the data (unless all the data are already in IML vectors or matrices).&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 17:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233501#M2465</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-11-06T17:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233511#M2466</link>
      <description>&lt;P&gt;In other words, with the &amp;nbsp;MERGE statement, the rows in two datasets &lt;STRONG&gt;will&lt;/STRONG&gt; line up? (The new dataset is calculated based on the old one, so has the same number of rows, but does not share a common field with it.)&lt;/P&gt;&lt;P&gt;Sorry for these kinds of questions, I'm utterly new to SAS and IML.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 18:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233511#M2466</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T18:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233513#M2467</link>
      <description>&lt;P&gt;Yes. Here is a simple example that you can study and play with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
do i = 1 to 10;
   x = i + 1;
   y = -2*i -4;
   output;
end;
run;

proc iml;
newVar = T(10:1);  /* 10, 9, 8,..., 1 */
create Calc var "newVar";
append;
close Calc;
quit;

data Want;
merge Have Calc;
run;
 
proc print data=Want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Nov 2015 18:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233513#M2467</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-11-06T18:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233514#M2468</link>
      <description>&lt;P&gt;It is the programmers responsibility to make sure the rows line up. SAS will not guarantee this. If you have done nothing in either IML or outside of IML to change the orders of the rows, then they should line up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can guarantee that the rows will line up if you have some sort of row identifier variable, then you can merge the datasets using a BY statement and including this row identifier varialbe in the BY statement. For this to work, you need to have the row identifier in IML as well as in the original data set.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 18:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233514#M2468</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-11-06T18:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233519#M2469</link>
      <description>&lt;P&gt;So, to guarantee that MERGE works properly, do I need the new dataset to have two columns: the new content and the common field&amp;nbsp;(to use in the "BY" statement) from the old dataset?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 19:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233519#M2469</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T19:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233521#M2470</link>
      <description>&lt;P&gt;Either that, or by confirming that your code has not changed the order of the data in IML and has not changed the order of the data in your data step.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 19:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233521#M2470</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-11-06T19:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233527#M2471</link>
      <description>&lt;P&gt;Into the Calc dataset, I also want to read the variable "key_num" (or whatever I'll call the common field from the Have dataset) for use in the "BY" statement in the later MERGE. How do I make sure that the values in "newVar" line up with the corresponding values in "key_num"?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 19:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233527#M2471</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T19:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233529#M2472</link>
      <description>&lt;P&gt;You create your matrix in IML from the two columns in your original data set, key_num and the variable of interest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then when you use IML to create a new SAS data set, it will have key_num and the computed column. If there is any need to sort in IML, you'd want to sort both key_num and the data.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 19:25:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233529#M2472</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-11-06T19:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233540#M2473</link>
      <description>&lt;P&gt;How do I perform a computation (for example, elementwise multiplication) on &lt;STRONG&gt;only&lt;/STRONG&gt; that column of the matrix which holds the variable of interest? In other words, how do I avoid changing the key value during computations?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 20:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233540#M2473</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T20:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233545#M2474</link>
      <description>&lt;P&gt;Are you sure that you need IML for this problem?&amp;nbsp; Perhaps the DATA step would be easier for you,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to learn IML, here are some tips for &lt;A href="http://blogs.sas.com/content/iml/2014/08/11/ten-tips-for-learning-sasiml.html" target="_self"&gt;getting started with IML programming&lt;/A&gt;.&amp;nbsp; I think&amp;nbsp;after you complete steps 1-3 you should be able to make a good first attempt at this problem.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 20:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233545#M2474</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-11-06T20:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233546#M2475</link>
      <description>&lt;P&gt;I, too, am wondering whether IML is how this is done, since I'm not really doing anything that necessitates matrices (all my calculations are elementwise). What I need to do is multiply every value in a column by a certain number; raise every value in a column to a certain power; multiply each value by the corresponding value in another column; and obtain a sum of all the values in a column. Can all of these be done with the Data step?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 20:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233546#M2475</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-06T20:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233556#M2476</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61189"&gt;@dvtarasov&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I, too, am wondering whether IML is how this is done, since I'm not really doing anything that necessitates matrices (all my calculations are elementwise). What I need to do is multiply every value in a column by a certain number; raise every value in a column to a certain power; multiply each value by the corresponding value in another column; and obtain a sum of all the values in a column. Can all of these be done with the Data step?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So let's place the two constants into macro variables to make it easier to modify later.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let coeffient=1.5;
%let power=3 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it sounds like you want to do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  new_var = &amp;amp;coefficient*VAR1 * (VAR2 ** &amp;amp;power);
run;

proc means data=want sum;
  var new_var ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Nov 2015 20:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233556#M2476</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-06T20:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233589#M2477</link>
      <description>Thanks! I've experimented with this way of doing things and it seems to work for me. I am wondering, however: is it possible, for the OUTPUT statement of PROC MEANS, to produce, not a new dataset, but just a new variable that stores the sum?</description>
      <pubDate>Sat, 07 Nov 2015 01:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233589#M2477</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-07T01:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233595#M2478</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61189"&gt;@dvtarasov&lt;/a&gt; wrote:&lt;BR /&gt;Thanks! I've experimented with this way of doing things and it seems to work for me. I am wondering, however: is it possible, for the OUTPUT statement of PROC MEANS, to produce, not a new dataset, but just a new variable that stores the sum?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure what that means. &amp;nbsp;A variable is an attribute or question that has a value recorded for every observation. The sum of a column is not another variable, at least not at the same unit of measure as the original records in the source table.&lt;/P&gt;
&lt;P&gt;Now if you want and easy method to generate a sum over a table (or a by group within a table) and then assign that sum as essentially a constant it is easier to do in PROC SQL. &amp;nbsp;In fact it is easier in SAS's PROC SQL than in most vendors SQL implementation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create new as
   select id
        , cost
        , sum(cost) as total_cost
        , cost/ calculated total_cost as percent_cost
  from have
  group by id
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Nov 2015 02:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233595#M2478</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-07T02:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233640#M2479</link>
      <description>&lt;P&gt;Yes, obtaining the sum over a column (not grouped by anything within the column) and then using that sum as, essentially, a constant in the next step is what I'm trying to do.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before trying this with my actual data, I experimented on a small "test" dataset which had two columnts, Var1 and Var2. I tried to obtain the sum ("total_cost") of Var1 and then write a new dataset, which would be like the old one but have an additinal column, Var3,&amp;nbsp;that would hold the values of Var2 multiplied by "total_cost". The summing was done as follows:&lt;/P&gt;&lt;PRE&gt;proc sql;
select sum(var1) as total_cost
from sasdata.olddata;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This part worked; the expected sum was computed. By the next step, however, "total_cost" seems to have gone missing and could not be used in the calculation:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data sasdata.newdata;
set sasdata.olddata;
Var3 = total_cost * Var2;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NEWDATA&amp;nbsp;was indeed created, but the Var3 column is blank "as a result of performing an operation on missing values." For some reason the new dataset also had a "total_cost" column after that, even though I did not ask for such a column. It, too, was blank.&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems, then, that "total_cost" (what's the term for it, if not "variable?") is no longer available by the time the next step starts. How do I pass it on to the next step?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Nov 2015 16:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233640#M2479</guid>
      <dc:creator>dvtarasov</dc:creator>
      <dc:date>2015-11-07T16:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Populating a new column in an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233642#M2480</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61189"&gt;@dvtarasov&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Yes, obtaining the sum over a column (not grouped by anything within the column) and then using that sum as, essentially, a constant in the next step is what I'm trying to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before trying this with my actual data, I experimented on a small "test" dataset which had two columnts, Var1 and Var2. I tried to obtain the sum ("total_cost") of Var1 and then write a new dataset, which would be like the old one but have an additinal column, Var3,&amp;nbsp;that would hold the values of Var2 multiplied by "total_cost". The summing was done as follows:&lt;/P&gt;
&lt;PRE&gt;proc sql;
select sum(var1) as total_cost
from sasdata.olddata;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This part worked; the expected sum was computed. By the next step, however, "total_cost" seems to have gone missing and could not be used in the calculation:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data sasdata.newdata;
set sasdata.olddata;
Var3 = total_cost * Var2;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NEWDATA&amp;nbsp;was indeed created, but the Var3 column is blank "as a result of performing an operation on missing values." For some reason the new dataset also had a "total_cost" column after that, even though I did not ask for such a column. It, too, was blank.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems, then, that "total_cost" (what's the term for it, if not "variable?") is no longer available by the time the next step starts. How do I pass it on to the next step?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So the first PROC SQL step just produced a report. &amp;nbsp;It did not generate anything that could be saved for later use because you did not ask it to do that. &amp;nbsp;It did not report any of the other variables in the old data either since you did not include them in the select statement. &amp;nbsp;You can make a new dataset with the total as new column and copy forward all of the data by using a statement like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table newdata as 
   select *,sum(var1) as total_cost
   from olddata
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The reason your data step made an empty variable is because the only reference to the variable was on the right hand side fo the assignment statement. So SAS saw that you were referencing an previously undefined variable and created one for you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a method that you can use to calculate and use a total in a single data step. But doing this means that you have to take control of when SAS reads and writes observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newdata ;
   if _n_=1 then do while  (not eof);
      set olddata end=eof;
      total_cost = sum(total_cost,var1);
   end;
   retain total_cost;
   set olddata ;
   var3 = total_cost * var2;
run;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically the IF statement/ DO loop will make it read in the whole dataset when the data step first starts and sum var1 into the new varaible TOTAL_COST. The rest of the data step will process normally.&lt;/P&gt;</description>
      <pubDate>Sat, 07 Nov 2015 17:00:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populating-a-new-column-in-an-existing-dataset/m-p/233642#M2480</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-07T17:00:19Z</dc:date>
    </item>
  </channel>
</rss>

