<?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: How to insert into existing dataset with one of the columns being a sum of other columns? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781456#M31793</link>
    <description>Maybe SQL only recognize the number value ,not  sub-query of SQL.&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT sum(money)  into :sum  from table2  ;&lt;BR /&gt;&lt;BR /&gt;	INSERT INTO table1 (type, MONEY)&lt;BR /&gt;	VALUES("CD", &amp;amp;sum.)&lt;BR /&gt;	;&lt;BR /&gt;QUIT;</description>
    <pubDate>Sat, 20 Nov 2021 13:24:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-11-20T13:24:48Z</dc:date>
    <item>
      <title>How to insert into existing dataset with one of the columns being a sum of other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781378#M31761</link>
      <description>&lt;P&gt;I would like to do the following:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Sum up the column "Money" in table 2&lt;/LI&gt;&lt;LI&gt;insert a new row into table1&lt;/LI&gt;&lt;LI&gt;put NULL values in the other_cols&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have these two tables&lt;/P&gt;&lt;PRE&gt;TABLE 1
TYPE | MONEY | other_col 1 | other_col 2 | 
A | 1000 | stuff | more
B | 500 | stuff | ok&lt;/PRE&gt;&lt;PRE&gt;TABLE 2
TYPE | MONEY | 
C | 750| 
D | 100 | &lt;/PRE&gt;&lt;P&gt;And Im trying to do a SQL statement that turns table 1 into:&lt;/P&gt;&lt;PRE&gt;TABLE 1 (Updated)
TYPE | MONEY | other_col 1 | other_col 2 | 
A | 1000 | stuff | more
B | 500 | stuff | ok
CD | 850 | NULL | NULL&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Nov 2021 20:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781378#M31761</guid>
      <dc:creator>heyyou1</dc:creator>
      <dc:date>2021-11-19T20:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert into existing dataset with one of the columns being a sum of other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781381#M31762</link>
      <description>&lt;P&gt;I'm trying to do something like this, but I'm not sure if you can nest statements&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
	INSERT INTO table1 (type, MONEY)
	VALUES("CD",  SELECT sum(money) from table2)
	;
QUIT;&lt;/PRE&gt;&lt;P&gt;But I get this error&lt;/P&gt;&lt;PRE&gt; PROC SQL;
 183        INSERT INTO race_sjc_mph_data (race, dose_1_num)
 184        VALUES("Other Race/ Unknown",  SELECT sum(dose_1_num) from race_unknown_other)
                                            ______
                                            22
                                            76
 ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, 
               a missing value, ), +, ',', -, MISSING, NULL, USER.  
 
 ERROR 76-322: Syntax error, statement will be ignored.&lt;/PRE&gt;&lt;P&gt;* Notice the variables and tables are name differently. I wanted to be clear with my question *&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 20:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781381#M31762</guid>
      <dc:creator>heyyou1</dc:creator>
      <dc:date>2021-11-19T20:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert into existing dataset with one of the columns being a sum of other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781399#M31769</link>
      <description>&lt;P&gt;Try this :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.TABLE_1;
LENGTH TYPE $ 10 MONEY 8 
       other_col_1 $ 8 other_col_2 $ 8;
infile datalines delimiter='|';
input TYPE $ MONEY other_col_1 $ other_col_2 $;
datalines;
A | 1000 | stuff | more
B | 500  | stuff | ok
;
run;

data work.TABLE_2;
LENGTH TYPE $ 10 MONEY 8;
infile datalines delimiter='|';
input TYPE $ MONEY;
datalines;
C | 750 
D | 100 
;
run;

data work.table_3(drop=TYPE MONEY);
LENGTH TYPE $ 10 ret_type $ 10 MONEY 8 ret_money 8 
       other_col_1 $ 8 other_col_2 $ 8;
 set work.TABLE_2 end=last;
 retain ret_type ret_money;
 ret_type  = strip(ret_type) !! strip(type);
 ret_money = SUM(ret_money , money);
 other_col_1 = 'NULL';
 other_col_2 = 'NULL';
 if last then output;
run;

PROC APPEND base=work.TABLE_1 data=work.TABLE_3(rename=(ret_type=type ret_money=money));
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 21:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781399#M31769</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-19T21:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert into existing dataset with one of the columns being a sum of other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781425#M31776</link>
      <description>&lt;P&gt;Don't do that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all it will mess up you input data to insert ANY new observations.&amp;nbsp; So make a NEW dataset, don't insert&amp;nbsp; data into the existing dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second if you have mixed types of records then the resulting dataset will be impossible to use for anything other than printing.&amp;nbsp; In which case just print the original data and have the reporting procedure include the sum.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=table1;
  var type money;
  sum money;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or for your second example use a FORMAT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
  tables race ;
  format race racef. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 03:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781425#M31776</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-20T03:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert into existing dataset with one of the columns being a sum of other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781456#M31793</link>
      <description>Maybe SQL only recognize the number value ,not  sub-query of SQL.&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT sum(money)  into :sum  from table2  ;&lt;BR /&gt;&lt;BR /&gt;	INSERT INTO table1 (type, MONEY)&lt;BR /&gt;	VALUES("CD", &amp;amp;sum.)&lt;BR /&gt;	;&lt;BR /&gt;QUIT;</description>
      <pubDate>Sat, 20 Nov 2021 13:24:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-into-existing-dataset-with-one-of-the-columns/m-p/781456#M31793</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-20T13:24:48Z</dc:date>
    </item>
  </channel>
</rss>

