<?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 Check the presence of variable when a join the table from within the proc sql before joining? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377010#M276685</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a&lt;EM&gt;&lt;STRONG&gt; final_table&lt;/STRONG&gt;&lt;/EM&gt; that I need to create after joining some other tables.&lt;/P&gt;&lt;P&gt;I am dealing with large number of different structured xmls, So everytime whenever I create my &lt;EM&gt;&lt;STRONG&gt;final_table&lt;/STRONG&gt;&lt;/EM&gt; I would like to know if there is a way that I can check the variable is present or not before doing the join inside the proc sql.&lt;/P&gt;&lt;P&gt;My actual scenario is :&lt;/P&gt;&lt;PRE&gt;proc sql ;
	create table final_table  AS
		select
			
			 a.customer
			,a.year
			,a.var1
			,a.var2
			,a.var3
			,b.*
			,c.*
			
	from work.final_output as a 
        inner join prod.scores as b on a.id = b.id
	inner join prod.city as c on a.business_id=c.id
	
quit;&lt;/PRE&gt;&lt;P&gt;But in my current XML var1,var2 and var3 are not present. And I get the following error&lt;/P&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var1&amp;nbsp;could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var2 could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var3 could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;So I would like to know if I can check the presence of variable when a join the table from within the proc sql.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Please help&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Thanks in advance!&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 18 Jul 2017 14:05:19 GMT</pubDate>
    <dc:creator>jjames1</dc:creator>
    <dc:date>2017-07-18T14:05:19Z</dc:date>
    <item>
      <title>Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377010#M276685</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a&lt;EM&gt;&lt;STRONG&gt; final_table&lt;/STRONG&gt;&lt;/EM&gt; that I need to create after joining some other tables.&lt;/P&gt;&lt;P&gt;I am dealing with large number of different structured xmls, So everytime whenever I create my &lt;EM&gt;&lt;STRONG&gt;final_table&lt;/STRONG&gt;&lt;/EM&gt; I would like to know if there is a way that I can check the variable is present or not before doing the join inside the proc sql.&lt;/P&gt;&lt;P&gt;My actual scenario is :&lt;/P&gt;&lt;PRE&gt;proc sql ;
	create table final_table  AS
		select
			
			 a.customer
			,a.year
			,a.var1
			,a.var2
			,a.var3
			,b.*
			,c.*
			
	from work.final_output as a 
        inner join prod.scores as b on a.id = b.id
	inner join prod.city as c on a.business_id=c.id
	
quit;&lt;/PRE&gt;&lt;P&gt;But in my current XML var1,var2 and var3 are not present. And I get the following error&lt;/P&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var1&amp;nbsp;could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var2 could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: Column var3 could not be found in the table/view identified with the correlation name a.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;So I would like to know if I can check the presence of variable when a join the table from within the proc sql.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Please help&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Thanks in advance!&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 18 Jul 2017 14:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377010#M276685</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-18T14:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377013#M276686</link>
      <description>&lt;P&gt;Well, you can look at sashelp.vcolumn or dictionary.columns (same thing later is SQL syntax). &amp;nbsp;However the question is, how are you going to code for data you don't know? &amp;nbsp;What happens if its a different type, or needs further processing. &amp;nbsp;Step 1 about any programming with SAS is to know your data as SAS is a data programming language.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 14:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377013#M276686</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-18T14:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377017#M276687</link>
      <description>&lt;P&gt;Is it always the same "var#" pattern that you want to check for, or are there specific variable names that may or may not be there?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can always query the metadata to see if a variable or a set of variables is there and then inside of a macro conditionally execute that portion of the select statement. You could also create a macro variable from the metadata that would only have the variables actually present in your data and then use that in your code. For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
SELECT cats('a.', name) INTO :dynamicSelectVars
FROM sashelp.vcolumn
WHERE libname = 'WORK' and
               memname = 'FINAL_OUTPUT' and
               upcase(name)  eqt 'VAR';
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You'll need to be careful if none of the variables are in there, as it won't generate a macro variable, so you might want to initialize it to null. But given the details you've given, that's one approach.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 14:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377017#M276687</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-18T14:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377018#M276688</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Thank you for the quick reply&lt;/P&gt;&lt;P&gt;I have done the coding part for handling my exceptions for the different types of XMLs that I receive.&lt;/P&gt;&lt;P&gt;In some cases I dont have the value for this three variables so I would just like to know if I could do this during joining the table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 14:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377018#M276688</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-18T14:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377031#M276689</link>
      <description>&lt;P&gt;You can always just use "a.*" and get everything in the dataset, so it won't fail if those variables are not there. However, you might get more than you want and you'll need to be careful about naming conflicts with the other tables.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 14:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377031#M276689</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-18T14:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377041#M276690</link>
      <description>&lt;P&gt;Yes, I have a lot of variables if I use a.*&lt;/P&gt;&lt;P&gt;So filtering them out wont be very easy &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 15:06:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377041#M276690</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-18T15:06:44Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377044#M276691</link>
      <description>&lt;P&gt;yes there are specific variable names&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 15:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377044#M276691</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-18T15:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377116#M276692</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I tried the following&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
SELECT cats('a.', name) INTO :dynamicSelectVars separated by ','
FROM sashelp.vcolumn
WHERE libname = 'WORK' and
               memname = 'FINAL_OUTPUT' and
               upcase(name)  in  ('x1','y1','z1');
quit;

%put &amp;amp;dynamicSelectVars;
proc sql ;
	create table final_table  AS
		select
			
			 a.customer
			,a.year
			,&amp;amp;dynamicSelectVars
			,b.*
			,c.*
			
	from work.final_output as a 
        inner join prod.scores as b on a.id = b.id
	inner join prod.city as c on a.business_id=c.id
	
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Here this code works when I have any of the variables a.x1 or a.y1 or a.z1&lt;/P&gt;&lt;P&gt;But if I dont have any of the variables this gives me the same error that I have shown in my original post.&lt;/P&gt;&lt;P&gt;So can you help on how to handle this exception?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thankl you !!&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 20:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377116#M276692</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-18T20:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377120#M276693</link>
      <description>&lt;P&gt;For one, the following needs to be addressed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;               upcase(name)  in  ('x1','y1','z1');

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Should be:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;               upcase(name)  in  ('X1','Y1',Z1');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or make it lowcase, not upcase.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If none of those exist in the data set, then the macro variable will not be created, so you might want to initialize the macro variable to null before you try to create it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dynamicSelectVars =;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 20:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377120#M276693</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-18T20:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377122#M276694</link>
      <description>&lt;P&gt;The leading comma will also be an issue in the event the macro variable is null, too.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 20:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377122#M276694</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-18T20:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377384#M276695</link>
      <description>&lt;P&gt;I'm not sure if you're still dealing with this, but an approach that will resolve the issue with null macro variables and trailing commas is just to query all of your "a" vars from metadata. You know that customer and year will always be there, so the only question is the others:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
SELECT cats('a.', name) INTO :dynamicSelectVars separated by ','
FROM sashelp.vcolumn
WHERE libname = 'WORK' and
               memname = 'FINAL_OUTPUT' and
               lowcase(name)  in  ('customer', 'year', 'x1','y1','z1');
quit;

%put &amp;amp;dynamicSelectVars;
proc sql ;
	create table final_table  AS
		select &amp;amp;dynamicSelectVars
			, b.*
			, c.*
			
	from work.final_output as a 
        inner join prod.scores as b on a.id = b.id
	inner join prod.city as c on a.business_id=c.id
	
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jul 2017 15:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377384#M276695</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-19T15:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377404#M276696</link>
      <description>&lt;P&gt;Thank you so much for your reply&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What If I need to check the presence of variables and arrange them in my final_output?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
SELECT cats('a.', name) INTO :dynamicSelectVar1 separated by ','
FROM sashelp.vcolumn
WHERE libname = 'WORK' and
               memname = 'FINAL_OUTPUT' and
               name  in  ('x1','y1','z1');
quit;
%put &amp;amp;dynamicSelectVar1;

proc sql noprint;
SELECT cats('a.', name) INTO :dynamicSelectVar2 separated by ','
FROM sashelp.vcolumn
WHERE libname = 'WORK' and
               memname = 'FINAL_OUTPUT' and
               name  in  ('x2','y2');
quit;
%put &amp;amp;dynamicSelectVar2;


proc sql ;
	create table final_table  AS
		select
			 a.customer
			,a.year
			,&amp;amp;dynamicSelectVar1
			,b.*
                        ,&amp;amp;dynamicSelectVar2
			,c.*
			
	from work.final_output as a 
        inner join prod.scores as b on a.id = b.id
	inner join prod.city as c on a.business_id=c.id
	
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 15:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377404#M276696</guid>
      <dc:creator>jjames1</dc:creator>
      <dc:date>2017-07-19T15:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Check the presence of variable when a join the table from within the proc sql before joining?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377448#M276697</link>
      <description>&lt;P&gt;If the final order of the variables in the table is that critical, the solution is going to get more complicated/convoluted. Splitting into two separate queries to separate those variables will cause issues when one of them returns a null value. What I would do is use my last approach and then use the metadata on your final table to pull all the variables into a single macro variable ordered as you want. But again, this is going to get fairly convoluted to get the exact ordering you want.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 16:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-the-presence-of-variable-when-a-join-the-table-from-within/m-p/377448#M276697</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-07-19T16:42:53Z</dc:date>
    </item>
  </channel>
</rss>

