<?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 Creating a 'keep' list of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477368#M286210</link>
    <description>&lt;P&gt;I have a dataset whose column names are col_Dec16, col_Jan17, col_Feb17 and so on.&lt;/P&gt;&lt;P&gt;I have created 18 variables to hold a selection of&amp;nbsp;column names&amp;nbsp;as the report I need is to cover 18 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; report_date = '15May2018'd;&lt;/FONT&gt;&lt;BR /&gt;data_null_;
call symput('a01', cat('col_', put(intnx('month',&amp;amp;month1_date,2), monname3.), put(intnx('year',&amp;amp;report_date,-2), year2.))); /* Dec 2 years previous */ 
call symput('a02', cat('col_', put(intnx('month',&amp;amp;month1_date,3), monname3.), put(intnx('year',&amp;amp;report_date,-1), year2.))); /* Jan 1 years previous */
call symput('a03', cat('col_', put(intnx('month',&amp;amp;month1_date,4), monname3.), put(intnx('year',&amp;amp;report_date,-1), year2.))); /* Feb */
etc.....&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to create another set of variables&amp;nbsp;that contain a list of the months I want to keep, depending on the month of reporting.&lt;/P&gt;&lt;P&gt;Something like;&lt;/P&gt;&lt;P&gt;if month(report_date) =&amp;nbsp;May then keeplist = (&amp;amp;a01 &amp;amp;a02 &amp;amp;a03);&lt;/P&gt;&lt;P&gt;if month(report_date) =&amp;nbsp;April then keeplist = (&amp;amp;a01 &amp;amp;a02);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if I can do it in a LET statement or if it has to be a macro.&lt;/P&gt;&lt;P&gt;Here's what I've tried&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro keeplist;
%if month(&amp;amp;report_date) = 6 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a18;
%if month(&amp;amp;report_date) = 5 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a17;
%if month(&amp;amp;report_date) = 4 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a16;
%mend keeplist;

%keeplist;

proc sql; create table test (keep= &amp;amp;varlist2) as 
select *
from items a 
left outer join history b	
on a.ID = b.ID
quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I get this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Required operator not found in expression: month(&amp;amp;report_date) = 6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another attempt using %LET does allow me to specify the correct columns, but I need to make this conditional on the month.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let keeplist = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a16;

proc sql; create table test (keep= &amp;amp;keeplist) as
select *
from items a 
left outer join history b 
on a.ID = b.ID
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;FONT face="Courier New" size="3"&gt;Can anyone advise how best to do this? Thanks in advance&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jul 2018 08:35:57 GMT</pubDate>
    <dc:creator>catmad</dc:creator>
    <dc:date>2018-07-12T08:35:57Z</dc:date>
    <item>
      <title>Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477368#M286210</link>
      <description>&lt;P&gt;I have a dataset whose column names are col_Dec16, col_Jan17, col_Feb17 and so on.&lt;/P&gt;&lt;P&gt;I have created 18 variables to hold a selection of&amp;nbsp;column names&amp;nbsp;as the report I need is to cover 18 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; report_date = '15May2018'd;&lt;/FONT&gt;&lt;BR /&gt;data_null_;
call symput('a01', cat('col_', put(intnx('month',&amp;amp;month1_date,2), monname3.), put(intnx('year',&amp;amp;report_date,-2), year2.))); /* Dec 2 years previous */ 
call symput('a02', cat('col_', put(intnx('month',&amp;amp;month1_date,3), monname3.), put(intnx('year',&amp;amp;report_date,-1), year2.))); /* Jan 1 years previous */
call symput('a03', cat('col_', put(intnx('month',&amp;amp;month1_date,4), monname3.), put(intnx('year',&amp;amp;report_date,-1), year2.))); /* Feb */
etc.....&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to create another set of variables&amp;nbsp;that contain a list of the months I want to keep, depending on the month of reporting.&lt;/P&gt;&lt;P&gt;Something like;&lt;/P&gt;&lt;P&gt;if month(report_date) =&amp;nbsp;May then keeplist = (&amp;amp;a01 &amp;amp;a02 &amp;amp;a03);&lt;/P&gt;&lt;P&gt;if month(report_date) =&amp;nbsp;April then keeplist = (&amp;amp;a01 &amp;amp;a02);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if I can do it in a LET statement or if it has to be a macro.&lt;/P&gt;&lt;P&gt;Here's what I've tried&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro keeplist;
%if month(&amp;amp;report_date) = 6 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a18;
%if month(&amp;amp;report_date) = 5 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a17;
%if month(&amp;amp;report_date) = 4 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a16;
%mend keeplist;

%keeplist;

proc sql; create table test (keep= &amp;amp;varlist2) as 
select *
from items a 
left outer join history b	
on a.ID = b.ID
quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I get this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Required operator not found in expression: month(&amp;amp;report_date) = 6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another attempt using %LET does allow me to specify the correct columns, but I need to make this conditional on the month.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let keeplist = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a16;

proc sql; create table test (keep= &amp;amp;keeplist) as
select *
from items a 
left outer join history b 
on a.ID = b.ID
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;FONT face="Courier New" size="3"&gt;Can anyone advise how best to do this? Thanks in advance&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 08:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477368#M286210</guid>
      <dc:creator>catmad</dc:creator>
      <dc:date>2018-07-12T08:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477374#M286211</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can &amp;amp;report_date be in any of the 18 months?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If so, what should the keep list be if it is in the first month and what should the keep list be if it is in the last month?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If not, then what range of months can &amp;amp;report_date be in and could it be in a different year compared to the most recent month?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 09:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477374#M286211</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-07-12T09:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477376#M286212</link>
      <description>No - the report date is 'today' and all of the data relates to previous months. So, a report for today (12th July) would include all months between June 2018 and Jan2017 (18 months)</description>
      <pubDate>Thu, 12 Jul 2018 09:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477376#M286212</guid>
      <dc:creator>catmad</dc:creator>
      <dc:date>2018-07-12T09:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477380#M286213</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So would the keep list always have&amp;nbsp;18 variables, e.g., for today&amp;nbsp;col_jan17,...,col_jun18?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 10:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477380#M286213</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-07-12T10:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477387#M286214</link>
      <description>&lt;P&gt;I&amp;nbsp;have got something to work, but it may not be the most efficient method !&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
if month(&amp;amp;report_date) = 1   and year(&amp;amp;report_date) = 2017 then call symput('k01', "&amp;amp;a01");					/* In Jan, keep Dec */
if month(&amp;amp;report_date) = 2   and year(&amp;amp;report_date) = 2017 then call symput('k01', "&amp;amp;a01 &amp;amp;a02");			/* In Feb, keep Dec &amp;amp; Jan */
if month(&amp;amp;report_date) = 3   and year(&amp;amp;report_date) = 2017 then call symput('k01', "&amp;amp;a01 &amp;amp;a02 -- &amp;amp;a03");	/* In Mar, keep Dec, Jan &amp;amp; Feb */
etc etc

data with_history (keep=&amp;amp;k01);
set history;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jul 2018 10:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477387#M286214</guid>
      <dc:creator>catmad</dc:creator>
      <dc:date>2018-07-12T10:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477401#M286215</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To your original question/error, the below code errors because the MONTH() function is not part of the macro language, it's part of the data step language:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro keeplist;
%if month(&amp;amp;report_date) = 6 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a18;
%if month(&amp;amp;report_date) = 5 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a17;
%if month(&amp;amp;report_date) = 4 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a16;
%mend keeplist;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Luckily, the macro language has a function, %sysfunc, which calls functions from the data step language.&amp;nbsp; So the below will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro keeplist;
%global varlist2;
%if %sysfunc(month(&amp;amp;report_date)) = 6 %then %let varlist2 = &amp;amp;a01 &amp;amp;a02 -- &amp;amp;a13 &amp;amp;a14 -- &amp;amp;a18;
...
%mend keeplist;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the added %global statement, if this were not there VARLIST2 would be created as a local macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I would not recommend this approach. It seems like if you transpose your data, so that instead of having one column for each month you have one record for each month, it would become much easier.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 11:16:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477401#M286215</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-07-12T11:16:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477405#M286216</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following data step just creates a macro variable for the keep list, not individual macro variables for each column (which should be easy enough to do; just ask if also required):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let report_date = '15May2018'd;

data _null_;
   length keep_list $ 200;

   do i = -18 to -1;
      keep_list = cat(strip(keep_list), ' ', cat('col_', put(intnx('month',&amp;amp;report_date,i), monyy5.)));
   end;

   call symput('keep_list',keep_list);
run;

%put keep_list = &amp;amp;keep_list;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 11:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477405#M286216</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-07-12T11:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a 'keep' list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477419#M286217</link>
      <description>&lt;P&gt;Most likely, you designed the process to be difficult by your choice of column names.&amp;nbsp; The problem would become much easier to solve (I think) if you were to get rid of these names:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;col_Dec16 col_Jan17 ColFeb17&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use these names instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;col_201612 col_201701 col_201702&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-keep-list-of-variables/m-p/477419#M286217</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-12T12:34:11Z</dc:date>
    </item>
  </channel>
</rss>

