<?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: %let ListDates  and run multiple case when statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616777#M180630</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;, it is a bad idea to use formatted dates. You asked a similar question some time ago (&lt;A href="https://communities.sas.com/t5/SAS-Programming/VarList-macro-variable/m-p/614679#M179756" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/VarList-macro-variable/m-p/614679#M179756&lt;/A&gt;), and in the answer there you can see a similar solution with the date list read from a data set with real sas dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you really want to use the formatted dates in a macro variable to directly generate the case statements, this is possible too. Here is a solution which works in only one pass over input, no macro loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Date list;
%let ListDates=20NOV2019+25NOV2019+02DEC2019;

* Generate case statements;
data _null_;
	length date $9 str $32000;
	i = 0;
	do until (date='');
		i = i + 1;
		date = scan("&amp;amp;ListDates",i,'+');
		if date ne '' then str = catx(',',str,'sum(case when Date&amp;lt;"'||date||'"d then SumLoan else 0 end ) as Accum_Til_'||date);
	end;
	call symput('caselist',trim(str));
run;
%put &amp;amp;=caselist;

* Test the generated statements 1 - make test data;
data have;
	format date date9.;
	do ID = 1 to 3;
		do date = '10nov2019'd to '10dec2019'd;
			SumLoan = 12345 * ID;
			output;
		end;
	end;
run;

* Test the generated statements 2 - use in proc sql to see it actually working;
proc sql;
	create table want as
		select ID,
		&amp;amp;caselist
	from have
	group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 12 Jan 2020 11:49:29 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2020-01-12T11:49:29Z</dc:date>
    <item>
      <title>%let ListDates  and run multiple case when statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616767#M180623</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There is a macro variable called&amp;nbsp;ListDates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let ListDates=20NOV2019+25NOV2019+02DEC2019;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create automatically&amp;nbsp; following statements:&lt;/P&gt;
&lt;P&gt;sum(case when date&amp;lt;'20NOV2019'd then SumLoan else 0 end ) as AccumTil_20NOV2019,&lt;BR /&gt;sum(case when date&amp;lt;'25NOV2019'd then SumLoan else 0 end ) as AccumTil_25NOV2019,&lt;BR /&gt;sum(case when date&amp;lt;'02DEC2019'd then SumLoan else 0 end ) as AccumTil_02DEC2019,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see If in&amp;nbsp; &amp;amp;ListDates there are more arguments then more&amp;nbsp; sum case when statements will be created.&lt;/P&gt;
&lt;P&gt;Then I will use these statements in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;create table wanted as&lt;BR /&gt;select ID,&lt;BR /&gt;sum(case when date&amp;lt;'20NOV2019'd then SumLoan else 0 end ) as AccumTil_20NOV2019,&lt;BR /&gt;sum(case when date&amp;lt;'25NOV2019'd then SumLoan else 0 end ) as AccumTil_25NOV2019,&lt;BR /&gt;sum(case when date&amp;lt;'02DEC2019'd then SumLoan else 0 end ) as AccumTil_02DEC2019&lt;BR /&gt;from RawTbl&lt;BR /&gt;group by ID&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2020 10:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616767#M180623</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-01-12T10:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: %let ListDates  and run multiple case when statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616774#M180628</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is definitely a bad idea to store formatted dates in macrovariables.&lt;/P&gt;
&lt;P&gt;Is it a requirement in your case?&lt;/P&gt;
&lt;P&gt;Can't you use those dates in a SAS dataset ?&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2020 11:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616774#M180628</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-12T11:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: %let ListDates  and run multiple case when statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616776#M180629</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a way to achieve this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this help.&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dates;
	input date:date9.;
	datalines;
20NOV2019
25NOV2019
02DEC2019
;
run;

data _null_;
	set dates;
	call symputx("date"||left(_n_),date);
	call symputx("datef"||left(_n_),put(date,date9.));
	call symputx("nobs",_n_);
run;

%macro want();

	%do i=1 %to &amp;amp;nobs;
		proc sql;
			create table wanted_&amp;amp;i as
			select ID,
				   sum(case when date&amp;lt;&amp;amp;&amp;amp;date&amp;amp;i then SumLoan else 0 end) as AccumTil_&amp;amp;&amp;amp;datef&amp;amp;i
			from RawTbl
			group by ID	;
		quit;
	%end;
	
	data wanted;
		merge wanted_:;
		by ID;
	run;
%mend;

%want&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Jan 2020 11:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616776#M180629</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-12T11:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: %let ListDates  and run multiple case when statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616777#M180630</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;, it is a bad idea to use formatted dates. You asked a similar question some time ago (&lt;A href="https://communities.sas.com/t5/SAS-Programming/VarList-macro-variable/m-p/614679#M179756" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/VarList-macro-variable/m-p/614679#M179756&lt;/A&gt;), and in the answer there you can see a similar solution with the date list read from a data set with real sas dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you really want to use the formatted dates in a macro variable to directly generate the case statements, this is possible too. Here is a solution which works in only one pass over input, no macro loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Date list;
%let ListDates=20NOV2019+25NOV2019+02DEC2019;

* Generate case statements;
data _null_;
	length date $9 str $32000;
	i = 0;
	do until (date='');
		i = i + 1;
		date = scan("&amp;amp;ListDates",i,'+');
		if date ne '' then str = catx(',',str,'sum(case when Date&amp;lt;"'||date||'"d then SumLoan else 0 end ) as Accum_Til_'||date);
	end;
	call symput('caselist',trim(str));
run;
%put &amp;amp;=caselist;

* Test the generated statements 1 - make test data;
data have;
	format date date9.;
	do ID = 1 to 3;
		do date = '10nov2019'd to '10dec2019'd;
			SumLoan = 12345 * ID;
			output;
		end;
	end;
run;

* Test the generated statements 2 - use in proc sql to see it actually working;
proc sql;
	create table want as
		select ID,
		&amp;amp;caselist
	from have
	group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2020 11:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616777#M180630</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-12T11:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: %let ListDates  and run multiple case when statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616783#M180633</link>
      <description>&lt;P&gt;Drop your macro list obsession.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dates;
	input date:date9.;
	datalines;
20NOV2019
25NOV2019
02DEC2019
;
run;

data _null_;
set dates end=done;
if _n_ = 1 then call execute('proc sql; create table wanted as select id');
call execute(',sum(case when date&amp;lt;'!!put(date,5.)!!' then sumloan else 0 end) as accumtil_'!!put(date,yymmddn8.));
if done then call execute(' from rawtbl group by id;quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See how simple the code becomes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ups, meant to be an answer to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2020 13:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/let-ListDates-and-run-multiple-case-when-statements/m-p/616783#M180633</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-01-12T13:36:42Z</dc:date>
    </item>
  </channel>
</rss>

