<?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: Column selection in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64649#M14055</link>
    <description>you also do &lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * from udv(keep=trx1--trx24);&lt;BR /&gt;
quit; &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
otherwise, the macro can be limited to:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select trx1 %macro loop; %do i=2 %to 24; ,trx&amp;amp;i  %end;%mend;%loop  from one;&lt;BR /&gt;
quit;</description>
    <pubDate>Wed, 18 Aug 2010 01:06:48 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2010-08-18T01:06:48Z</dc:date>
    <item>
      <title>Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64646#M14052</link>
      <description>if we have 24 months of data in 24 columns like trx1 trx2 ..trx24  there is a way which is convenient in datastep to select those columns like trx1--trx24.&lt;BR /&gt;
&lt;BR /&gt;
in proc sql we have to type each column name in the statement like&lt;BR /&gt;
a.trx1,a.trx2 ..a.trx24.&lt;BR /&gt;
&lt;BR /&gt;
is there a way to avoid manual typing and have an easy way to select columns?</description>
      <pubDate>Mon, 16 Aug 2010 20:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64646#M14052</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-08-16T20:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64647#M14053</link>
      <description>SASPhile&lt;BR /&gt;
&lt;BR /&gt;
Not sure if this is easy but it gets the job done and can be made for flexible too.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
data one;&lt;BR /&gt;
&lt;BR /&gt;
  array trx(24);&lt;BR /&gt;
&lt;BR /&gt;
  do i=1 to 1000;&lt;BR /&gt;
    do j=1 to 24;&lt;BR /&gt;
      trx&lt;J&gt;=ranuni(1);&lt;BR /&gt;
	end;&lt;BR /&gt;
    output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
  proc sql;&lt;BR /&gt;
    select trx1 %do i=2 %to 24;&lt;BR /&gt;
	              ,trx&amp;amp;i&lt;BR /&gt;
	   %end;&lt;BR /&gt;
    from one;&lt;BR /&gt;
%mend test;&lt;BR /&gt;
%test;&lt;/J&gt;</description>
      <pubDate>Tue, 17 Aug 2010 12:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64647#M14053</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2010-08-17T12:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64648#M14054</link>
      <description>I tried similar way.I thought if there is something that is close to datastep way of declaring the fields.</description>
      <pubDate>Tue, 17 Aug 2010 16:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64648#M14054</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-08-17T16:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64649#M14055</link>
      <description>you also do &lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * from udv(keep=trx1--trx24);&lt;BR /&gt;
quit; &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
otherwise, the macro can be limited to:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select trx1 %macro loop; %do i=2 %to 24; ,trx&amp;amp;i  %end;%mend;%loop  from one;&lt;BR /&gt;
quit;</description>
      <pubDate>Wed, 18 Aug 2010 01:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64649#M14055</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2010-08-18T01:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64650#M14056</link>
      <description>I would recommend Chris's solution, but "gild the lilly" a little more. &lt;BR /&gt;
Useful in more than just proc sql, what is needed here is a loop that will generate the pattern &lt;BR /&gt;
  something{n}else&lt;BR /&gt;
where "something else" is constant and n is a counter&lt;BR /&gt;
 &lt;BR /&gt;
This might generate a string useful in many places&lt;BR /&gt;
[pre]%macro gen1( n, pattern= something###else, from=1, by=1 ) ;&lt;BR /&gt;
%local i ;&lt;BR /&gt;
%do i= &amp;amp;from %to &amp;amp;n %by &amp;amp;by ;&lt;BR /&gt;
%sysfunc( tranwrd( %str(&amp;amp;pattern), ###, &amp;amp;i ))&lt;BR /&gt;
%end ;&lt;BR /&gt;
%mend  gen1 ;[/pre]&lt;BR /&gt;
Use that like&lt;BR /&gt;
%put %gen1(24, pattern=###) ;&lt;BR /&gt;
OR&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
  select trx1, %gen1( 24, PATTERN=%str( , trx###), from=2 ) )&lt;BR /&gt;
     from txn.dataset&lt;BR /&gt;
 ; &lt;BR /&gt;
beware that second demo is untested&lt;BR /&gt;
  &lt;BR /&gt;
we need to be careful how to make sure that the pattern is "data" to the macro rather than resolve looking like syntax to the macro - so I used %str()&lt;BR /&gt;
other times I might use %superq()&lt;BR /&gt;
 &lt;BR /&gt;
other times, I've found it useful to apply a format like Z3. to the &amp;amp;i&lt;BR /&gt;
 &lt;BR /&gt;
the attention is on the macro language handling strings, rather than on the sas syntax for a data step or proc.

no matter how much I review, allways it seems to need a corrction&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: Peter.C</description>
      <pubDate>Wed, 18 Aug 2010 06:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64650#M14056</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-08-18T06:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: Column selection in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64651#M14057</link>
      <description>You use the following idea to get the names and then the list of variables:&lt;BR /&gt;
DATA TEST;&lt;BR /&gt;
      DROP I;&lt;BR /&gt;
      ARRAY D(*) D1-D24;&lt;BR /&gt;
      DO I = 1 TO 24 BY 1;&lt;BR /&gt;
            D(I) = I;&lt;BR /&gt;
      END;   &lt;BR /&gt;
      OUTPUT;&lt;BR /&gt;
      STOP;   RETURN;                                                                                                          RUN;                                                                                                                                     &lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
       CREATE TABLE NAMES AS&lt;BR /&gt;
              SELECT NAME&lt;BR /&gt;
               FROM DICTIONARY.COLUMN                                                                                                                                       &lt;BR /&gt;
      WHERE UPCASE(LIBNAME) EQ 'WORK'                                                                                                   &lt;BR /&gt;
        AND UPCASE(MEMNAME)='TEST'                                                                                                      &lt;BR /&gt;
        AND UPCASE(TYPE)='NUM'                                                                                                          &lt;BR /&gt;
        AND UPCASE(SUBSTR(NAME,1,1)) EQ 'D'                                                                                             &lt;BR /&gt;
   ;                                                                                                                                    &lt;BR /&gt;
   SELECT DISTINCT NAME                                                                                                                 &lt;BR /&gt;
      INTO :NAMEVAR SEPARATED BY ', '                                                                                                   &lt;BR /&gt;
      FROM WORK.NAMES                                                                                                                   &lt;BR /&gt;
   ;                                                                                                                    &lt;BR /&gt;
QUIT;                                                                                                                                   &lt;BR /&gt;
%PUT NAMEVAR=&amp;amp;NAMEVAR;&lt;BR /&gt;
&lt;BR /&gt;
The macro variable NAMEVAR contains the list of variable that you need.&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
       SELECT &amp;amp;NAMEVAR</description>
      <pubDate>Wed, 18 Aug 2010 18:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-selection-in-proc-sql/m-p/64651#M14057</guid>
      <dc:creator>MikeM</dc:creator>
      <dc:date>2010-08-18T18:53:04Z</dc:date>
    </item>
  </channel>
</rss>

