<?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 Reference a Sequence of Columns in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758853#M239684</link>
    <description>&lt;P&gt;If I want to use PROC SQL to join to datasets, what is a quick way to reference a sequence of columns?&lt;/P&gt;&lt;P&gt;That is, if dataset 1 has columns: ID, NAME, X1, X2, X3, X4, X5 and dataset 2 has columns:&amp;nbsp; ID, NAME, Y1, Y2, Y3, Y4, Y5, then how can I join data1.* with columns Y1 to Y5 on ID and NAME using PROC SQL? Is there a way similar to data steps Y1--Y5? Or would I need to write out every column?&lt;/P&gt;</description>
    <pubDate>Mon, 02 Aug 2021 17:48:09 GMT</pubDate>
    <dc:creator>mariko5797</dc:creator>
    <dc:date>2021-08-02T17:48:09Z</dc:date>
    <item>
      <title>Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758853#M239684</link>
      <description>&lt;P&gt;If I want to use PROC SQL to join to datasets, what is a quick way to reference a sequence of columns?&lt;/P&gt;&lt;P&gt;That is, if dataset 1 has columns: ID, NAME, X1, X2, X3, X4, X5 and dataset 2 has columns:&amp;nbsp; ID, NAME, Y1, Y2, Y3, Y4, Y5, then how can I join data1.* with columns Y1 to Y5 on ID and NAME using PROC SQL? Is there a way similar to data steps Y1--Y5? Or would I need to write out every column?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 17:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758853#M239684</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-08-02T17:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758855#M239685</link>
      <description>&lt;P&gt;The Y1-Y5 syntax, and the Y1--Y5 syntax, do not work in PROC SQL. You have to write out all of the columns names, or (if possible) use something like data1.* and data2.*&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 17:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758855#M239685</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-02T17:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758878#M239688</link>
      <description>&lt;P&gt;data1.* and data2.* does not work unless you rename the variables ID and Name in either dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using DICTIONARY.COLUMNS is an alternative, but it fails to be as satisfying as the syntax X1--X5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
  ID=_N_;
  set SAShelp.Fish (Keep=Species Length: obs=3);
  rename 
    Species=Name
    Length1=X1
    Length2=X2
    Length3=X3
;
data data2;
  ID=_N_;
  set SAShelp.Fish (Keep=Species Length: firstobs=4 obs=6);
  rename 
    Species=Name
    Length1=Y1
    Length2=Y2
    Length3=Y3
;
run;

proc sql noprint;
  select cats(memname,".",name)
    into :data1_X1_X3 separated by ","
    from dictionary.columns
    where LIBNAME="WORK"
    and memname = "DATA1"
      and name like "X_";
  select cats(memname,".",name)
    into :data2_Y1_Y3 separated by ","
    from dictionary.columns
    where LIBNAME="WORK"
    and memname = "DATA2"
      and name like "Y_";
quit;
  %put &amp;amp;=data1_X1_X3 ;
  %put &amp;amp;=data2_Y1_Y3;

proc sql;
  select data1.ID, 
      data1.name, 
      &amp;amp;data1_X1_X3, 
      &amp;amp;data2_Y1_Y3
    from data1, data2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Aug 2021 12:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758878#M239688</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-08-03T12:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758888#M239696</link>
      <description>&lt;P&gt;When I have an excessively large list of variables, I copy the list (column-wise) into excel, create another column that is simply , and then concatenate the variable names with the comma.&amp;nbsp; Then I copy the list with commas back into SAS.&amp;nbsp; It's not clever, but it's simple, fast, and works.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 20:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758888#M239696</guid>
      <dc:creator>tellmeaboutityo</dc:creator>
      <dc:date>2021-08-02T20:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758918#M239706</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/311553"&gt;@mariko5797&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots of good ideas here.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way is the little quickie macro I have written below.&amp;nbsp; See test code below that -- that you should be able to use on your system.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use, you just need to call the macro, tell it the variable name, and specify how many numbered occurrences there are.&amp;nbsp; Like so:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Generate_Vars(Length, 3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the above example, my variable name is "Length", and there are three occurrences.&amp;nbsp; The following will be generated:&lt;/P&gt;
&lt;PRE&gt;Length1
,Length2
,Length3&lt;/PRE&gt;
&lt;P&gt;Note that the first occurrence is&amp;nbsp;&lt;STRONG&gt;not&amp;nbsp;&lt;/STRONG&gt;preceded with a comma.&amp;nbsp; The macro will put a comma before all iterations except the first.&amp;nbsp; It should work with either preceding or trailing commas.&amp;nbsp; I prefer preceding commas, so the example is formatted that way, but either should work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can turn off the commas (if for some reason you needed to) by passing COMMA=NO as a third parameter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO	Generate_Vars(Var, Count, Comma=YES);
	%LOCAL	Separator;
	%IF	%QUPCASE(&amp;amp;Comma)	=	YES	%THEN
		%LET	Separator	=	%STR(,);
	%DO	i					=	1	%TO	&amp;amp;Count;
		%IF	&amp;amp;i				=	1	%THEN
			%DO;
				&amp;amp;Var&amp;amp;i
			%END;
		%ELSE
			%DO;
				&amp;amp;Separator&amp;amp;Var&amp;amp;i
			%END;
	%END;
%MEND	Generate_Vars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test code (uses SAShelp Libname, so it should work in any system):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC	SQL;
	SELECT
		Height
		,%Generate_Vars(Length, 3)
		,Species
		,Weight
		,Width
		FROM	SASHELP.Fish;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 23:46:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758918#M239706</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-02T23:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758932#M239710</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/311553"&gt;@mariko5797&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If I want to use PROC SQL to join to datasets, what is a quick way to reference a sequence of columns?&lt;/P&gt;
&lt;P&gt;That is, if dataset 1 has columns: ID, NAME, X1, X2, X3, X4, X5 and dataset 2 has columns:&amp;nbsp; ID, NAME, Y1, Y2, Y3, Y4, Y5, then how can I join data1.* with columns Y1 to Y5 on ID and NAME using PROC SQL? Is there a way similar to data steps Y1--Y5? Or would I need to write out every column?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Am I missing something here?&amp;nbsp; What is being overlooked in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table want as 
  select * from data1,data2 where data1.name=data2.name and data1.id=data2.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or if you want to keep missing right-side and missing left-side contributions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table want as 
  select * from data1 full join data2 on data1.name=data2.name and data1.id=data2.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if what you are really saying is that there are dozens of unwanted variables in data1 and/or data2, then yes SQL would require you to construction variable lists instead of the "select *".&amp;nbsp; But PROC SQL is using sas datasets, and the sas dataset engine honors sas dataset name parameters like KEEP= and DROP=. which do provide some shorthand list syntax, as in:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table tt as 
  select * from data1 (keep=id name x1-x3) 
               ,data2 (keep=id name y:)
   where data1.name=data2.name and data1.id=data2.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table tt as 
  select * from data1 (keep=id name--x3) 
               ,data2 (keep=id name y:)
   where data1.name=data2.name and data1.id=data2.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 01:38:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758932#M239710</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-08-03T01:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758946#M239723</link>
      <description>&lt;P&gt;Ah.&amp;nbsp; Very good.&amp;nbsp; Yes, just code SAS shorthands on the Drop and Keep.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see you threw in three techniques, all good:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Single hyphen: x1 - x3 (all variables starting with, in this case, the letter "x" in sequence, starting with the number 1 through the last of the sequence as specified)&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Double hyphen:&amp;nbsp; name -- x3 (all variables, based on position, between and including name and x3)&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; Wildcard:&amp;nbsp; y: (all variables starting with a "y")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My little macro could still be useful for explicit pass through queries, although, truth be told, it was just fun to write.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 04:27:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/758946#M239723</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-03T04:27:33Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/759006#M239760</link>
      <description>&lt;P&gt;Should we, or should we not request this in a SASware ballot?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select data1.ID, 
      data1.name, 
      of data1.X1 - data1.X3,
      of data2.Y1 -- data2.Y3
    from data1, data2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Aug 2021 12:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/759006#M239760</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-08-03T12:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a Sequence of Columns in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/759020#M239768</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Should we, or should we not request this in a SASware ballot?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select data1.ID, 
      data1.name, 
      of data1.X1 - data1.X3,
      of data2.Y1 -- data2.Y3
    from data1, data2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;or maybe&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select data1.ID, 
      data1.name, 
      (of data1.X1 - data1.X3),
      (of data2.Y1 -- data2.Y3)
    from data1, data2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Interesting either way.&amp;nbsp; Not earth shattering, but that would definitely make coding easier.&amp;nbsp; I say submit it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 13:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-a-Sequence-of-Columns-in-PROC-SQL/m-p/759020#M239768</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-03T13:34:38Z</dc:date>
    </item>
  </channel>
</rss>

