I would like to do the following:
For example, I have these two tables
TABLE 1 TYPE | MONEY | other_col 1 | other_col 2 | A | 1000 | stuff | more B | 500 | stuff | ok
TABLE 2 TYPE | MONEY | C | 750| D | 100 |
And Im trying to do a SQL statement that turns table 1 into:
TABLE 1 (Updated) TYPE | MONEY | other_col 1 | other_col 2 | A | 1000 | stuff | more B | 500 | stuff | ok CD | 850 | NULL | NULL
I'm trying to do something like this, but I'm not sure if you can nest statements
PROC SQL; INSERT INTO table1 (type, MONEY) VALUES("CD", SELECT sum(money) from table2) ; QUIT;
But I get this error
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.
* Notice the variables and tables are name differently. I wanted to be clear with my question *
Try this :
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 */
Koen
Don't do that.
First of all it will mess up you input data to insert ANY new observations. So make a NEW dataset, don't insert data into the existing dataset.
Second if you have mixed types of records then the resulting dataset will be impossible to use for anything other than printing. In which case just print the original data and have the reporting procedure include the sum.
proc print data=table1;
var type money;
sum money;
run;
Or for your second example use a FORMAT.
proc freq data=have;
tables race ;
format race racef. ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.