<?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: PROC SQL Case colulmn exists or does not exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468603#M119716</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132557"&gt;@iSAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello KurtBremser. My problem is the first suggestion you've mentioned.&lt;BR /&gt;&lt;BR /&gt;I'm receiving a source file and its column/s changes consistently. Example, today I will receive the file with Col1,Col2, Col3, Col4 and tomorrow I will only receive Col1,Col2, and Col4. Thus I would like to create a program that:&lt;BR /&gt;If a certain column exists on the current source file, it will get the data of that column. Otherwise, it will just put null.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See this example, using sashelp.class as a guinea pig:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* list of columns needed */
data columns_needed;
input name :$32. type :$1.;
name = upcase(name);
cards;
weight n
age n
sex c
;
run;

proc sort data=columns_needed;
by name;
run;

/* create some example data */
data have;
set sashelp.class (drop=age);
run;

/* retrieve metadata */
proc sql;
create table columns as
select upcase(name) as name
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE'
order by name;
quit;

/* compare existing columns with needed, and dynamically create a data step to create missing columns */
data _null_;
merge
  columns (in=have)
  columns_needed (in=need)
  end=eof
;
by name;
if _n_ = 1
then call execute('data want; set have;');
if need and not have
then do;
  if type = 'n'
  then call execute(name !! " = .;");
  else call execute(name !! " = '';");
end;
if eof then call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Jun 2018 06:05:49 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-06-08T06:05:49Z</dc:date>
    <item>
      <title>PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468310#M119596</link>
      <description>&lt;P&gt;Hello. I would like to create a program something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;create table TARGET as&lt;/P&gt;&lt;P&gt;Select&lt;/P&gt;&lt;P&gt;&amp;nbsp; case when column1 exists then get the value of column 1. Otherwise null end as COL1,&lt;/P&gt;&lt;P&gt;&amp;nbsp; case when column2 exists then get the value of column 2. Otherwise null end as COL2,&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;From SOURCE;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May I know how I can convert this to sas/proc sql code? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2018 09:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468310#M119596</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2018-06-07T09:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468318#M119598</link>
      <description>&lt;P&gt;Since the existence of a column will be known before the step, I'd retrieve that information from dictionary.columns and create code selectively.&lt;/P&gt;
&lt;P&gt;If, OTOH, you are looking for missing values, the solution would be different. Which of the two is it?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2018 10:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468318#M119598</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-07T10:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468321#M119599</link>
      <description>&lt;P&gt;Really a good idea to post test data in the form of a datastep and what you want out.&amp;nbsp; Now I am guessing that you don't mean, then the column is present or not, but when the data within the column is present or not?&amp;nbsp; If so your code is nearly there (note how I use the code window - its the {i} above post are):&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table target as
  select case when col1 ne "" then col1 else "" end as col1,
         case when col2 ne "" then col2 else "" end as col2
  from have;
quit;
&lt;/PRE&gt;
&lt;P&gt;Note this assumes the variables are character, numeric would be ne .&amp;nbsp; However there doesn't seem to be any point to this code.&amp;nbsp; Simply:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table target as
  select col1,
         col2
  from have;
quit;&lt;/PRE&gt;
&lt;P&gt;Will achieve exactly the same thing, you don't need to set missing's explicitly.&amp;nbsp; Maybe you meant something else, but I cant' tell from what you posted.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2018 10:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468321#M119599</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-07T10:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468562#M119697</link>
      <description>Hello KurtBremser. My problem is the first suggestion you've mentioned.&lt;BR /&gt;&lt;BR /&gt;I'm receiving a source file and its column/s changes consistently. Example, today I will receive the file with Col1,Col2, Col3, Col4 and tomorrow I will only receive Col1,Col2, and Col4. Thus I would like to create a program that:&lt;BR /&gt;If a certain column exists on the current source file, it will get the data of that column. Otherwise, it will just put null.</description>
      <pubDate>Fri, 08 Jun 2018 01:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468562#M119697</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2018-06-08T01:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468563#M119698</link>
      <description>Hello RW9. Thank you for the response.&lt;BR /&gt;&lt;BR /&gt;My issue is somewhat different: Example, today I will receive the file with Col1,Col2, Col3, Col4 and tomorrow I will only receive Col1,Col2, and Col4. Thus I would like to create a program that:&lt;BR /&gt;If a certain column exists on the current source file, it will get the data of that column. Otherwise, it will just put null.</description>
      <pubDate>Fri, 08 Jun 2018 01:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468563#M119698</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2018-06-08T01:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468603#M119716</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132557"&gt;@iSAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello KurtBremser. My problem is the first suggestion you've mentioned.&lt;BR /&gt;&lt;BR /&gt;I'm receiving a source file and its column/s changes consistently. Example, today I will receive the file with Col1,Col2, Col3, Col4 and tomorrow I will only receive Col1,Col2, and Col4. Thus I would like to create a program that:&lt;BR /&gt;If a certain column exists on the current source file, it will get the data of that column. Otherwise, it will just put null.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See this example, using sashelp.class as a guinea pig:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* list of columns needed */
data columns_needed;
input name :$32. type :$1.;
name = upcase(name);
cards;
weight n
age n
sex c
;
run;

proc sort data=columns_needed;
by name;
run;

/* create some example data */
data have;
set sashelp.class (drop=age);
run;

/* retrieve metadata */
proc sql;
create table columns as
select upcase(name) as name
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE'
order by name;
quit;

/* compare existing columns with needed, and dynamically create a data step to create missing columns */
data _null_;
merge
  columns (in=have)
  columns_needed (in=need)
  end=eof
;
by name;
if _n_ = 1
then call execute('data want; set have;');
if need and not have
then do;
  if type = 'n'
  then call execute(name !! " = .;");
  else call execute(name !! " = '';");
end;
if eof then call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Jun 2018 06:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468603#M119716</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-08T06:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468609#M119718</link>
      <description>&lt;P&gt;Then your problem is not with coding, but understanding the data and the modelling, and not having proper documentation on the data transfer - all common issues.&amp;nbsp; Why does the data structure change like this?&amp;nbsp; If indeed these columns change each time, then they should not be columns - they should be rows of data.&amp;nbsp; Columns are a fixed structure which can be agreed up front, and programmed against, rows, expand and shrink as the data needs.&amp;nbsp; This is basic data modelling.&amp;nbsp; So the sender should identify the data which changes and apply this in rows not columns, then you and the sender can agree a data transfer document which details the fixed structure - they can program the export and you the import based on the consistent agreed document.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The alternative is that you try to fix this time, then next time they do something else, maybe they wont call them col next time, maybe they wont number them like that, thus you repeat this exercise every import.&amp;nbsp; A slight change nullifies this:&lt;/P&gt;
&lt;P&gt;From&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Col1&amp;nbsp; Col2&amp;nbsp; Col3 Col4&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To&lt;/P&gt;
&lt;P&gt;Col&amp;nbsp; &amp;nbsp; &amp;nbsp;Result&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d&lt;/P&gt;
&lt;P&gt;This one can have 3 removed without any change to the import program or your coding which uses this data.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jun 2018 07:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468609#M119718</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-08T07:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468615#M119723</link>
      <description>Hello KurtBremser. I tried it and it worked!Thank you so much for the big help</description>
      <pubDate>Fri, 08 Jun 2018 08:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468615#M119723</guid>
      <dc:creator>iSAS</dc:creator>
      <dc:date>2018-06-08T08:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case colulmn exists or does not exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468617#M119725</link>
      <description>&lt;P&gt;Now you should heed &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;'s advice. If there's actually data hidden in the column names, then a proper modeling of the input data will make all this unnecessary.&lt;/P&gt;
&lt;P&gt;Or you simply do a transpose of your dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in;
input id col_1 col_2 col_4;
cards;
1 3 5 6
2 7 2 5
;
run;

proc transpose
  data=in
  out=out (rename=(_name_=colname col1=value))
;
by ID; /* add other variables that never change */
var col_:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you have normalized dataset that needs no correction in code at all.&lt;/P&gt;
&lt;P&gt;When you now merge that by colname to your dataset of needed columns (or any other dataset that has the required structure), the missing values will appear on their own.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jun 2018 08:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-colulmn-exists-or-does-not-exists/m-p/468617#M119725</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-08T08:33:45Z</dc:date>
    </item>
  </channel>
</rss>

