<?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: Using SAS to insert rows containing nulls and blanks into database in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/650733#M195162</link>
    <description>&lt;P&gt;Again s_lassen thanks for your answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For further reference I want to explain for other Sas-novices like me how solution 2 works:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* *** 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.&lt;BR /&gt;    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(&amp;lt;connect options&amp;gt;);'  /
    '  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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For debugging and understanding this code one can also write the result into a file (change&amp;nbsp;&lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;filename tempSas temp;&lt;/SPAN&gt;&amp;nbsp;to&amp;nbsp;&lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;filename tempsas disk "example.txt";&lt;/SPAN&gt;&amp;nbsp;and remove &lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;%include tempsas;&lt;/SPAN&gt;) and see the resulting code in that file.&lt;/P&gt;</description>
    <pubDate>Tue, 26 May 2020 13:26:46 GMT</pubDate>
    <dc:creator>bitterjn</dc:creator>
    <dc:date>2020-05-26T13:26:46Z</dc:date>
    <item>
      <title>Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647977#M193982</link>
      <description>&lt;P&gt;I posted this question already on &lt;A href="https://stackoverflow.com/questions/61794876/using-sas-to-insert-rows-containing-nulls-and-blanks-into-database" target="_self"&gt;stackoverflow&lt;/A&gt; but couldn't get an answer, so I hope that the experts here can help me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a db2 table&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;DBTable&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with columns&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;A, B, C&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(all of type varchar) which is linked to a library&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;lib&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;in SAS. I use SAS to generate a dataset&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;ValuesForA&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with one column whose content I want to write into the column&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;A&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;DBTable&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with the additional requirement that the the column for&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;B&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is filled with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;' '&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(blank) and the column for&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;C&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;(null)&lt;/CODE&gt;. So the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;DBTable&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;should look something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;|  A  |  B  |    C   |
======================
| 'x' | ' ' | (null) |
| 'y' | ' ' | (null) |
| 'z' | ' ' | (null) |&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I cannot find a way how to acchieve this as SAS as it treats blanks as null.&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;The simple approach specifying&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;B&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;as&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;" "&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;just fills this column with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;(null)&lt;/CODE&gt;. I also tried to use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;nullchar=no&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;option and not specifying a value for&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;C&lt;/CODE&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;proc sql;
   insert into lib.DBTable
      (nullchar=no, A, B)
   select
       A, " " as B
   from ValuesForA;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;however the column&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;C&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is then also filled with blanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;|  A  |  B  |  C  |
===================
| 'x' | ' ' | ' ' |
| 'y' | ' ' | ' ' |
| 'z' | ' ' | ' ' |&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 08:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647977#M193982</guid>
      <dc:creator>bitterjn</dc:creator>
      <dc:date>2020-05-15T08:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647980#M193984</link>
      <description>Worth a shot:  try hard coding the equivalent of a null  character as a literal string.  For a character field C,&lt;BR /&gt;&lt;BR /&gt;'00'x as C&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 15 May 2020 09:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647980#M193984</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-05-15T09:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647989#M193992</link>
      <description>&lt;P&gt;AFAIK, there is no way round that using a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see two possibilities:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a temporary table in DB2 (just the A values, the others are constant), and use pass-through SQL to insert the values in the permanent table.&lt;/LI&gt;
&lt;LI&gt;Write a temporary SAS program to insert the values using pass-through SQL.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Solution 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data lib.ValuesForA;
  set ValuesForA(keep=a);
run;

proc sql;
  connect to DB2 &amp;lt;connect options&amp;gt;;
  execute by DB2(
    insert into DBTable (a,b) select a,' ' from ValuesForA
    );
  drop table lib.ValuesForA;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Solution 2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Filename tempsas temp;

Data _null_;
  file tempsas;
  set ValuesForA end=done;
  if _N_=1 then put
    'proc sql;' /
    '  Connect to DB2 &amp;lt;connect options&amp;gt;;'  /
    '  execute by DB2(' /
    '    insert into DBTable (a,b)'
    ;
  put
    "       values ('" a +(-1) "',' ')" @;
  if done then put
    / '      );' /
     'quit;'
    ;
  else put ',';
run;

%include tempsas;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 10:37:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647989#M193992</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-15T10:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647991#M193993</link>
      <description>Thanks for the tipp &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;But it does not seem to work. Column C is still not filled with (null) (in DbVisualizer the character in column C is displayed as a square btw)</description>
      <pubDate>Fri, 15 May 2020 10:55:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647991#M193993</guid>
      <dc:creator>bitterjn</dc:creator>
      <dc:date>2020-05-15T10:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647993#M193995</link>
      <description>Thanks for the reply.&lt;BR /&gt;Solution 1: I think I would prefer this solution, but in my case that's not possible as I don't have rights to create tables in the database.&lt;BR /&gt;Solution 2: This seems to be a solution that stays on the SAS-side as long as it can, which for me is actually quite good. But I have troubles to understand the code. Can you point out to me where the content of ValuesForA get's transfered into the server session. I assume it's the line ending in an @, so maybe you can tell me what kind of syntax this is or where I can read about that. I don't want to hurt the companie's data base &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;</description>
      <pubDate>Fri, 15 May 2020 11:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647993#M193995</guid>
      <dc:creator>bitterjn</dc:creator>
      <dc:date>2020-05-15T11:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647996#M193997</link>
      <description>That could not be, SAS take NULL and BLANK as the same thing due to in sas BLANK is just NULL.&lt;BR /&gt;the only way do it is using Pass-Through as s_lassen did.</description>
      <pubDate>Fri, 15 May 2020 11:33:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/647996#M193997</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-15T11:33:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/650733#M195162</link>
      <description>&lt;P&gt;Again s_lassen thanks for your answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For further reference I want to explain for other Sas-novices like me how solution 2 works:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* *** 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.&lt;BR /&gt;    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(&amp;lt;connect options&amp;gt;);'  /
    '  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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For debugging and understanding this code one can also write the result into a file (change&amp;nbsp;&lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;filename tempSas temp;&lt;/SPAN&gt;&amp;nbsp;to&amp;nbsp;&lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;filename tempsas disk "example.txt";&lt;/SPAN&gt;&amp;nbsp;and remove &lt;SPAN style="background-color: #f5f5f5; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px;"&gt;%include tempsas;&lt;/SPAN&gt;) and see the resulting code in that file.&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 13:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/650733#M195162</guid>
      <dc:creator>bitterjn</dc:creator>
      <dc:date>2020-05-26T13:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS to insert rows containing nulls and blanks into database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/650867#M195203</link>
      <description>&lt;P&gt;What I normally do when writing temporary programs like this is&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;submit the code to create the program&lt;/LI&gt;
&lt;LI&gt;take a look at what I have generated ("include tempsas" on the command line in a SAS text editor, or open Explorer -&amp;gt; File Shortcuts, and take a look at the "Tempsas" file there&lt;/LI&gt;
&lt;LI&gt;If it looks OK, take a shot at submitting the code - in many cases, I have a number of statements in the file, so I start with first one to see if that works - in this particular case, the whole stuff is one SQL statement, so we have to do it in one fell swoop (I love that expression!).&lt;/LI&gt;
&lt;LI&gt;If this works OK, add the %include line to the final program&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 18:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-to-insert-rows-containing-nulls-and-blanks-into/m-p/650867#M195203</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-26T18:02:06Z</dc:date>
    </item>
  </channel>
</rss>

