<?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: Looping Dates as Macro Variables from a Table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566758#M11378</link>
    <description>&lt;P&gt;A slight variation of other persons code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data available_dates;
	input date_value date9.;
	format date_value date9.;
datalines;
05MAR19
06MAR19
07MAR19
08MAR19
;;;
run;
 
%macro bigCode(start_date);
	*Here you put your 5000 lines of code.;
	%put Do fun things with the startdate "&amp;amp;start_date";
%mend bigCode;

%macro dates (ds_name, var_name);
	proc sql noprint; 
		select count(*) into: ct
		from &amp;amp;ds_name;
	quit; 

	%do i = 1 %to &amp;amp;ct.;
		data _null_;
			set available_dates (firstobs=&amp;amp;i obs=&amp;amp;i); *Only read one line at each pass.;
			call symputx('start_date',&amp;amp;var_name); 
		run;
		**********************************************;
		* Call your big macro from here with &amp;amp;start_date as a parameter.;
		**********************************************;
		%bigCode (&amp;amp;start_date)
	%end;
%mend;

%dates (available_dates, date_value);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 18 Jun 2019 00:53:25 GMT</pubDate>
    <dc:creator>heffo</dc:creator>
    <dc:date>2019-06-18T00:53:25Z</dc:date>
    <item>
      <title>Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566738#M11374</link>
      <description>&lt;P&gt;I have a table &lt;STRONG&gt;&lt;EM&gt;available_dates&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;which has row of date values, like this:-&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;&lt;STRONG&gt;Table: available_dates&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;&lt;STRONG&gt;date_value&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;05MAR19&lt;BR /&gt;06MAR19&lt;BR /&gt;07MAR19&lt;BR /&gt;08MAR19&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a huge 5000+ lines of code that uses a macro_variable called &lt;STRONG&gt;&lt;EM&gt;&amp;amp;start_date. &lt;/EM&gt;&lt;/STRONG&gt;This macro-variable is used multiple times throughout the code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I want that entire code to run n times, where n is number of rows of date in table available_dates&amp;nbsp;(n = 4 in this case)&lt;/P&gt;&lt;P&gt;For every iteration, I want &lt;STRONG&gt;&lt;EM&gt;&amp;amp;start_date&lt;/EM&gt;&lt;/STRONG&gt; to be a specific date from the table &lt;STRONG&gt;&lt;EM&gt;available_dates&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, the code will first have &amp;amp;start_date = 05MAR19, then run all 5000 lines, then &amp;amp;start_date = 06MAR19, then run all 5000 lines, then &amp;amp;start_date = 07MAR19, then run all 5000 lines and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I this looping? I see a lot of posts to loop over strings, but this is a date value, so its getting a little tricky.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 22:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566738#M11374</guid>
      <dc:creator>Anirudh9</dc:creator>
      <dc:date>2019-06-17T22:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566743#M11376</link>
      <description>&lt;P&gt;I would capture the number of dates into a macro then use a loop to retrieve the dates.&lt;/P&gt;&lt;P&gt;The date needs to be changed to a number while in the macro.&lt;/P&gt;&lt;P&gt;This code will create a macro called &lt;STRONG&gt;&lt;EM&gt;start_date&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;that is the date from your available_dates set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select count(*) into: ct&lt;/P&gt;&lt;P&gt;from available_dates;&lt;/P&gt;&lt;P&gt;quit;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro dates;&lt;/P&gt;&lt;P&gt;%do i = 1 %to &amp;amp;ct.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;set&amp;nbsp;available_dates;&lt;/P&gt;&lt;P&gt;format date_value 12.;&lt;/P&gt;&lt;P&gt;if &amp;amp;i. = _n_ then call symput('&lt;STRONG&gt;&lt;EM&gt;start_date&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;',date_value);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%put &amp;amp;&lt;STRONG&gt;&lt;EM&gt;start_date&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;..... lines of code.......;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%dates;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 22:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566743#M11376</guid>
      <dc:creator>ehmsoleil</dc:creator>
      <dc:date>2019-06-17T22:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566751#M11377</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235217"&gt;@Anirudh9&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your big program is stored in an external .sas file below code how you can call this program multiple times, each time setting macro variable &amp;amp;start_date to a new value. Below approach doesn't require your big program being wrapped into a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample program */
filename code temp;
data _null_;
  file code;
  put
    'data _null_;'/
    '  put "Start_Date is: &amp;amp;start_date";'/
    '  stop;'/
    'run;'/
    ;
  stop;
run;

/* below code pretty much as you could use it */

/* 
  filename code '&amp;lt;full path &amp;amp; name of your .sas program&amp;gt;';
*/
data date_value;
  input start_date :$9.;
  datalines;
05MAR19
06MAR19
07MAR19
08MAR19
;

data _null_;
  set date_value;
  length cmd $200;
  cmd='data _null_;'||cats('call symputx("start_date",',"'",start_date,"'",');stop;run;');
  cmd=cats(cmd,'%include code / source2;');
  call execute(cmd);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 00:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566751#M11377</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-06-18T00:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566758#M11378</link>
      <description>&lt;P&gt;A slight variation of other persons code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data available_dates;
	input date_value date9.;
	format date_value date9.;
datalines;
05MAR19
06MAR19
07MAR19
08MAR19
;;;
run;
 
%macro bigCode(start_date);
	*Here you put your 5000 lines of code.;
	%put Do fun things with the startdate "&amp;amp;start_date";
%mend bigCode;

%macro dates (ds_name, var_name);
	proc sql noprint; 
		select count(*) into: ct
		from &amp;amp;ds_name;
	quit; 

	%do i = 1 %to &amp;amp;ct.;
		data _null_;
			set available_dates (firstobs=&amp;amp;i obs=&amp;amp;i); *Only read one line at each pass.;
			call symputx('start_date',&amp;amp;var_name); 
		run;
		**********************************************;
		* Call your big macro from here with &amp;amp;start_date as a parameter.;
		**********************************************;
		%bigCode (&amp;amp;start_date)
	%end;
%mend;

%dates (available_dates, date_value);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jun 2019 00:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566758#M11378</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-06-18T00:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566759#M11379</link>
      <description>&lt;P&gt;Personally I would convert the program into a macro.&amp;nbsp; Could be as simple as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro run_5000(start_date);
%include 'program_with_5000_lines.sas';
%mend run_5000;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call the macro once for each observation in the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set available_dates ;
  call execute(cats('%nrstr(%run_5000)(',put(date_value,date9.),')');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jun 2019 01:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566759#M11379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-18T01:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566804#M11385</link>
      <description>&lt;P&gt;Strongly second &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s suggestion; when data is already present in a dataset, there is no need to take the detour through a list of macro variables (which is not a good solution as such, anyway). call execute() is the way to go.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 07:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566804#M11385</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-18T07:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566825#M11391</link>
      <description>Hey, nerd, it is a FINE “solution as such.” Especially when someone is learning macros and needs the flexibility.</description>
      <pubDate>Tue, 18 Jun 2019 12:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566825#M11391</guid>
      <dc:creator>ehmsoleil</dc:creator>
      <dc:date>2019-06-18T12:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566902#M11404</link>
      <description>&lt;P&gt;This alone&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set available_dates;
format date_value 12.;
if &amp;amp;i. = _n_ then call symput('start_date ',date_value);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;shows that you should invest MUCH more time learning to work with Base SAS before wasting time with inefficient abuse of the macro preprocessor.&lt;/P&gt;
&lt;P&gt;The format statement is not necessary (call symput() will alway store the raw value, unless you force a format by using the put() function), and the data step will read the whole dataset in every iteration of the macro loop. Use the point= option to retrieve a single observation most efficiently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you want to ignore the advice from a combined ~50 years of SAS experience, that's up to you.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 14:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/566902#M11404</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-18T14:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/567056#M11428</link>
      <description>&lt;P&gt;I'm not 'ignoring' Tom's solution;it is beautiful and sophisticated. I'm addressing your rudeness about my suggestion.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You don't have to be so arrogant or dismissive; there are many ways to skin a cat. If someone is just learning macros/looping he or she may want to run a less sophisticated and advanced piece of code. That's all I'm saying.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 20:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/567056#M11428</guid>
      <dc:creator>ehmsoleil</dc:creator>
      <dc:date>2019-06-18T20:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping Dates as Macro Variables from a Table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/567061#M11430</link>
      <description>&lt;P&gt;Quote:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"Hey, nerd,"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I rest my case regarding rudeness.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 20:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-Dates-as-Macro-Variables-from-a-Table/m-p/567061#M11430</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-18T20:22:36Z</dc:date>
    </item>
  </channel>
</rss>

