I posted this question already on stackoverflow but couldn't get an answer, so I hope that the experts here can help me.
I have a db2 table DBTable
with columns A, B, C
(all of type varchar) which is linked to a library lib
in SAS. I use SAS to generate a dataset ValuesForA
with one column whose content I want to write into the column A
of DBTable
with the additional requirement that the the column for B
is filled with ' '
(blank) and the column for C
with (null)
. So the DBTable
should look something like this:
| A | B | C |
======================
| 'x' | ' ' | (null) |
| 'y' | ' ' | (null) |
| 'z' | ' ' | (null) |
I cannot find a way how to acchieve this as SAS as it treats blanks as null.
The simple approach specifying B
as " "
just fills this column with (null)
. I also tried to use the nullchar=no
option and not specifying a value for C
:
proc sql;
insert into lib.DBTable
(nullchar=no, A, B)
select
A, " " as B
from ValuesForA;
quit;
however the column C
is then also filled with blanks
| A | B | C |
===================
| 'x' | ' ' | ' ' |
| 'y' | ' ' | ' ' |
| 'z' | ' ' | ' ' |
AFAIK, there is no way round that using a data step.
I see two possibilities:
Solution 1:
Data lib.ValuesForA;
set ValuesForA(keep=a);
run;
proc sql;
connect to DB2 <connect options>;
execute by DB2(
insert into DBTable (a,b) select a,' ' from ValuesForA
);
drop table lib.ValuesForA;
quit;
Solution 2:
Filename tempsas temp;
Data _null_;
file tempsas;
set ValuesForA end=done;
if _N_=1 then put
'proc sql;' /
' Connect to DB2 <connect options>;' /
' execute by DB2(' /
' insert into DBTable (a,b)'
;
put
" values ('" a +(-1) "',' ')" @;
if done then put
/ ' );' /
'quit;'
;
else put ',';
run;
%include tempsas;
AFAIK, there is no way round that using a data step.
I see two possibilities:
Solution 1:
Data lib.ValuesForA;
set ValuesForA(keep=a);
run;
proc sql;
connect to DB2 <connect options>;
execute by DB2(
insert into DBTable (a,b) select a,' ' from ValuesForA
);
drop table lib.ValuesForA;
quit;
Solution 2:
Filename tempsas temp;
Data _null_;
file tempsas;
set ValuesForA end=done;
if _N_=1 then put
'proc sql;' /
' Connect to DB2 <connect options>;' /
' execute by DB2(' /
' insert into DBTable (a,b)'
;
put
" values ('" a +(-1) "',' ')" @;
if done then put
/ ' );' /
'quit;'
;
else put ',';
run;
%include tempsas;
Again s_lassen thanks for your answer.
For further reference I want to explain for other Sas-novices like me how solution 2 works:
/* *** Create a temporary 'document' with the name tempsas *** */
Filename tempsas temp;
/* *** Loop throuhg ValuesForA and write into tempsas. *** */
/* (The resulting tempsas is basically an SQL insert statement.
All observations are written in one big values statement) */
Data _null_;
file tempsas;
set ValuesForA end=done;
if _N_=1 then put
'rsubmit;' /
'proc sql;' /
' Connect to DB2(<connect options>);' /
' execute by DB2(' /
' insert into DBTable (a,b)'
' values'
;
put
" ('" a +(-1) "',' ')" @; /* "a" is the observation ValuesForA.a
"+(-1)" removes the trailing blank set by the put statement
"@" writes the next put statement in the same line
if done then put
/
' );' /
'quit;' /
'endrsubmit;'
;
else put ',';
run;
/* *** Run the code in tempsas *** */
%include tempsas;
For debugging and understanding this code one can also write the result into a file (change filename tempSas temp; to filename tempsas disk "example.txt"; and remove %include tempsas;) and see the resulting code in that file.
What I normally do when writing temporary programs like this is
But anyway, I am glad you could use my suggestion. Although I would also prefer the first solution. I think there is a way to create temporary tables in DB2 (as in most databases, you may want to talk to the DBAs about that), but you may not have the rights to do that either. But if the amount of data is not too large, the second solution should work OK.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.