<?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 where clause if variable exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666617#M199481</link>
    <description>This worked! Thank you</description>
    <pubDate>Thu, 02 Jul 2020 15:15:38 GMT</pubDate>
    <dc:creator>Len18</dc:creator>
    <dc:date>2020-07-02T15:15:38Z</dc:date>
    <item>
      <title>PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666128#M199254</link>
      <description>&lt;P&gt;I am trying to create a table where column A = 'Y', if column A does not exist then where column B= 'Y'. Some months the source table has column A and other months it has column B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attempted to use the coalescec function but this does no work if the both columns are not present.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE TBL_&amp;amp;DATE. AS
	SELECT 
		NAME,	
		ADDRESS,
		PRODUCT
	FROM 
		SRC_&amp;amp;DATE.
	WHERE 
		COALESCEC(COL_A,COL_B) = 'Y'
;QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS EG 7.1&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 15:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666128#M199254</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-06-30T15:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666129#M199255</link>
      <description>&lt;P&gt;try below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE TBL_&amp;amp;DATE. AS
	SELECT 
		NAME,	
		ADDRESS,
		PRODUCT
	FROM 
		SRC_&amp;amp;DATE.
	WHERE 
		COL_A='Y' or COL_B= 'Y'
;QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jun 2020 15:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666129#M199255</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-06-30T15:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666131#M199256</link>
      <description>&lt;P&gt;There is a big difference between "Column A does not exist" and "the value of column a is missing".&lt;/P&gt;
&lt;P&gt;"Does not exist" to me means that the variable is not in the data set and any reference attempting to use the variable will be an error, and rightfully so.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your process should be fixed so that the variable name is consistent. I would spend more time tracking down why and fixing it much earlier in the process. When I see one issue like this I usually see multiples. And each causes a different set of headaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are "reading" external data from a source that cannot have consistent column headers, typical examples are spreadsheets with poor control over contents, with Proc Import then you need to address how the data is read. Proc Import guesses each and every time it is called to read a file. And differences will appear such as variable types changing, variable names as you have seen, lengths of variables. All of these make programming difficult. Since you have mentioned sometimes one name and sometimes another then that strongly implies you need to control the data read as you go forward to prevent problems.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 15:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666131#M199256</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-30T15:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666142#M199261</link>
      <description>&lt;P&gt;Depending on your SAS version, this code may work for you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date=&amp;amp;sysdate;
data src_&amp;amp;date;
	NAME='Some Name';
	ADDRESS='Some Address';
	PRODUCT='Some Product';
	COL_A='Y';
	*COL_B='Y';
run;
%let g_dynWhere=;
options mprint;

PROC SQL NOPRINT;
	SELECT STRIP(name)||"='Y'"
	INTO	:g_dynWhere separated by ' OR ' 
	FROM	dictionary.columns
	WHERE	libname='WORK'
	AND		memname=upcase("SRC_&amp;amp;DATE")
	AND		name LIKE 'COL_%'
	AND		memtype='DATA';

	/*%put &amp;amp;=g_dynWhere;*/

	CREATE TABLE TBL_&amp;amp;DATE. AS
	SELECT 
		NAME,	
		ADDRESS,
		PRODUCT
	FROM 
		SRC_&amp;amp;DATE.
	%if (%superq(g_dynWhere) NE ) %then
	%do;
		WHERE &amp;amp;g_dynWhere
	%end;
	;
QUIT;	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Back in 2018,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;posted the following &lt;A title="Using %IF-%THEN-%ELSE in SAS programs" href="https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/" target="_blank" rel="noopener"&gt;Blog&lt;/A&gt; about SAS 9.4 M5. The code above would examine the structure of your source table and dynamically build the condition.&lt;/P&gt;
&lt;P&gt;That condition then in turn, would conditionally be used in the Create Table Query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 16:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666142#M199261</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-30T16:20:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666161#M199268</link>
      <description>Hi, yes, the variable is not always in the dataset. The source table is not within my control and although either column can be used for my purpose, the two columns are not identical. Ideally, I would have liked both columns to be in the dataset.</description>
      <pubDate>Tue, 30 Jun 2020 17:35:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666161#M199268</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-06-30T17:35:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666164#M199270</link>
      <description>Hi,&lt;BR /&gt;This gives the column not found error for either Column A or B depending on the month, as both columns do not exist in the source table each month.</description>
      <pubDate>Tue, 30 Jun 2020 17:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666164#M199270</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-06-30T17:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666193#M199283</link>
      <description>Hi I just tried it, it's giving me the full dataset and not just those containing 'Y' in the column.&lt;BR /&gt;&lt;BR /&gt;Just to ensure I follow what the code is doing though. You firstly created a dummy dataset. Then you created a null variable g_dynWhere. Then it's removing leading and trailing spaces and putting data from columns A &amp;amp; B into the null variable created previously. Since the 2 columns can now be referenced by the single variable we use it in the where clause by saying if it is not blank then we apply it to where clause.</description>
      <pubDate>Tue, 30 Jun 2020 19:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666193#M199283</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-06-30T19:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666195#M199285</link>
      <description>&lt;P&gt;I would suggest this kind of approach (a view and coalescec with a default value) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t1;
do id = "a", "b", "c", "";
    a = id;
    output;
    end;
run;

data t2;
do id = "a", "b", "c", "";
    b = id;
    output;
    end;
run;

proc sql;
create table tt
   (
    table char(2),
    id char(1),
    c char(12),
    whichColumn char(12)
   );
quit;

%macro insert(t);

data _t / view=_t;
length a b $12;
set &amp;amp;t;
run;

proc sql;
insert into tt (table, id, c, whichColumn) 
select 
    "&amp;amp;t." as table,
    id, 
    coalescec(a, b, "x") as c,
    choosec(1 + whichc(calculated c, a, b), "None", "Col a", "Col b") as whichColumn
from _t;
drop view _t;
quit;

%mend insert;

%insert(t1);
%insert(t2);

proc print data=tt noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;table 	id 	c 	whichColumn
t1 	a 	a 	Col a
t1 	b 	b 	Col a
t1 	c 	c 	Col a
t1 	  	x 	None
t2 	a 	a 	Col b
t2 	b 	b 	Col b
t2 	c 	c 	Col b
t2 	  	x 	None&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jun 2020 19:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666195#M199285</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-06-30T19:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666216#M199299</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324692"&gt;@Len18&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you got the most of it. Here is my break down&lt;/P&gt;
&lt;P&gt;1. Create sample/dummy data to simulate your initial situation, and provide working code.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;dictionary.columns is a table that contains the records about the structure of the table. Think of it, as a table representation of the SQL Describe tablename statement.&lt;/P&gt;
&lt;P&gt;3. Create Macro variable (g_dynWhere) that would hold a 'OR' separated string values of Col_A='Y' OR&amp;nbsp;Col_B='Y'. Col_A and Col_B are values stored in the name column.&lt;/P&gt;
&lt;P&gt;4. The condition&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if (%superq(g_dynWhere) NE ) %then
%do;
WHERE &amp;amp;g_dynWhere
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Would only add the WHERE clause if the resolved value of the&amp;nbsp; macro variable (g_dynWhere) was not blank. i.e. at least one of the two columns (Col_A, Col_B) existed in the Source Table with the required value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this clarify it for you&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 21:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666216#M199299</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-30T21:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666239#M199305</link>
      <description>Thanks a lot, yes this explains it perfectly.</description>
      <pubDate>Wed, 01 Jul 2020 01:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666239#M199305</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-07-01T01:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666269#M199319</link>
      <description>&lt;P&gt;SQL's rigid/declarative structure is unsuited to dealing with varying inputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;data TBL_&amp;amp;date.;
  length COL_A COL_B $1;
  set SRC_&amp;amp;date.; 	
  COL=coalescec(COL_A, COL_B)
  if COL = 'Y';
  keep NAME ADDRESS PRODUCT COL;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jul 2020 06:16:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666269#M199319</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-01T06:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666566#M199460</link>
      <description>&lt;P&gt;A simple macro could do the trick:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro extract(date);
  %local dsid varname;
  %let dsid=%sysfunc(open(SRC_&amp;amp;date,i));
  %if &amp;amp;dsid=0 %then %do;
    %put ERROR: TABLE SRC_&amp;amp;date could not be opened;
    %return;
    %end;
  %if %sysfunc(varnum(&amp;amp;dsid,COL_A)) %then 
    %let varname=COL_A;
  %else 
    %let varname=COL_B;
  %let dsid=%sysfunc(close(&amp;amp;dsid));
  PROC SQL;
    CREATE TABLE TBL_&amp;amp;DATE. AS
	SELECT 
		NAME,	
		ADDRESS,
		PRODUCT
	FROM 
		SRC_&amp;amp;DATE.
	WHERE 
		&amp;amp;varname='Y';
  QUIT;
%mend;&lt;BR /&gt;&lt;BR /&gt;%extract(02JUL2020);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro checks if COL_A is present on the input table and uses that if it is, otherwise COL_B.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 09:19:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666566#M199460</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-02T09:19:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL where clause if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666617#M199481</link>
      <description>This worked! Thank you</description>
      <pubDate>Thu, 02 Jul 2020 15:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-where-clause-if-variable-exists/m-p/666617#M199481</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-07-02T15:15:38Z</dc:date>
    </item>
  </channel>
</rss>

