<?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 Run macro for values in data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707189#M217117</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to run macro for values appear in another data set?&lt;/P&gt;
&lt;P&gt;In this case I want to run macro RRR for list of dates ppear in data set&amp;nbsp;&lt;CODE class=" language-sas"&gt;datesFormacro&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nnother question: How can I add the date to the summary data set?&lt;/P&gt;
&lt;P&gt;In this case I wrote&amp;nbsp;&lt;CODE class=" language-sas"&gt;summary&amp;amp;date. but&amp;nbsp;I&amp;nbsp;am&amp;nbsp;not&amp;nbsp;sure&amp;nbsp;if&amp;nbsp;it&amp;nbsp;works&amp;nbsp;fine&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data RawTb;
Input ID dates value;
Cards;
1 17Jun2019'd 10
2 17Jun2019'd 20
3 21Jun2019'd 30
4 21Jun2019'd 40
5 21Jun2019'd 50
6 18May2019'd 60
7 17Jun2019'd 70
8 18May2019'd 80
9 21Jun2019'd 90
;
Run;


%macro RRR(date);
Proc sql;
Create table  summary&amp;amp;date.    as
Select  &amp;amp;date.  As date,
                 sum(value) as sum_value
From  RawTb
;
Quit;
Proc print data= summary&amp;amp;date.noobs;run;
%mend RRR;
/*Question1-How can I add the date to data set name?*/
/*Is it okay to write  summary&amp;amp;date.*/

%RRR(date=17Jun2019'd);
%RRR(date=18May2019'd);


Data datesFormacro;
Input dates;
Cards;
17Jun2019'd
18May2019'd
;
Run;
/*Question2: How can I run the macro for the dates appear in data set datesFormacro?*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 19 Dec 2020 16:36:02 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2020-12-19T16:36:02Z</dc:date>
    <item>
      <title>Run macro for values in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707189#M217117</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to run macro for values appear in another data set?&lt;/P&gt;
&lt;P&gt;In this case I want to run macro RRR for list of dates ppear in data set&amp;nbsp;&lt;CODE class=" language-sas"&gt;datesFormacro&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nnother question: How can I add the date to the summary data set?&lt;/P&gt;
&lt;P&gt;In this case I wrote&amp;nbsp;&lt;CODE class=" language-sas"&gt;summary&amp;amp;date. but&amp;nbsp;I&amp;nbsp;am&amp;nbsp;not&amp;nbsp;sure&amp;nbsp;if&amp;nbsp;it&amp;nbsp;works&amp;nbsp;fine&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data RawTb;
Input ID dates value;
Cards;
1 17Jun2019'd 10
2 17Jun2019'd 20
3 21Jun2019'd 30
4 21Jun2019'd 40
5 21Jun2019'd 50
6 18May2019'd 60
7 17Jun2019'd 70
8 18May2019'd 80
9 21Jun2019'd 90
;
Run;


%macro RRR(date);
Proc sql;
Create table  summary&amp;amp;date.    as
Select  &amp;amp;date.  As date,
                 sum(value) as sum_value
From  RawTb
;
Quit;
Proc print data= summary&amp;amp;date.noobs;run;
%mend RRR;
/*Question1-How can I add the date to data set name?*/
/*Is it okay to write  summary&amp;amp;date.*/

%RRR(date=17Jun2019'd);
%RRR(date=18May2019'd);


Data datesFormacro;
Input dates;
Cards;
17Jun2019'd
18May2019'd
;
Run;
/*Question2: How can I run the macro for the dates appear in data set datesFormacro?*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Dec 2020 16:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707189#M217117</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-19T16:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Run macro for values in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707193#M217118</link>
      <description>&lt;P&gt;Maxim 28: Macro Variables Need No Formats.&lt;/P&gt;
&lt;P&gt;They are much easier to handle, but you need to create a human-readable value whenever needed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RRR(date);

/* prepare a human-readable date for the dataset name */
%local dsdate;
%let dsdate = %sysfunc(putn(&amp;amp;date.,yymmddn8.));

proc sql;
create table summary&amp;amp;dsdate. as
select
  &amp;amp;date. as date format=yymmdd10.,
  sum(value) as sum_value
from RawTb
;
quit;

proc print data=summary&amp;amp;dsdate. noobs;
run;

%mend RRR;

data datesFormacro;
input dates :date9.;
call execute(cats('%nrstr(%RRR(',dates,'))'));
cards;
17Jun2019
18May2019
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Dec 2020 18:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707193#M217118</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-19T18:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run macro for values in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707378#M217180</link>
      <description>&lt;P&gt;First problem is that you have unmatched quotes in your date values. As long as they are just data, it sort of works, but when you try to put them into unquoted macro variables, horrible things happen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The format of a SAS date constant is 'DDMonCCYY'd, not&amp;nbsp;DDMonCCYY'd (you have to have an initial quote as well).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can actually see the problem if you look at the code here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%RRR(date=17Jun2019'd);
%RRR(date=18May2019'd);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;- if you look at it in a SAS editor (or in your post after posting), you will see that the color coding shows that the text from the first quote to the last one is all considered as quoted text, so what you have is actually a single macro call like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%RRR(date=17Jun2019'd); %RRR(date=18May2019'd);&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words, the macro is called one single time with the date parameter having the value&lt;/P&gt;
&lt;P&gt;"17Jun2019'd); %RRR(date=18May2019'd" - the text inside the single quotes does not get resolved by the macro processor.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;already suggested, you should make your data set with SAS date values, not (semi-)quoted strings, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data RawTb;
informat dates date9.;
format dates date9.;
Input ID dates value;
Cards;
1 17Jun2019 10
2 17Jun2019 20
3 21Jun2019 30
4 21Jun2019 40
5 21Jun2019 50
6 18May2019 60
7 17Jun2019 70
8 18May2019 80
9 21Jun2019 90
;Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Same in the macro you should use formatted date values, so that you get a meaningful name for the output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RRR(date);
Proc sql;
Create table  summary&amp;amp;date.    as
Select  "&amp;amp;date."d  As date format=date9.,
                 sum(value) as sum_value
From  RawTb
;
Quit;
Proc print data= summary&amp;amp;date.noobs;run;
%mend RRR;

%RRR(date=17Jun2019);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your datesFormacro table should be created likewise:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data datesFormacro;
informat dates date9.;
format dates date9.;
Input dates;
Cards;
17Jun2019
18May2019
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use CALL Execute to generate the call, or you can write the code to a temporary file and %INCLUDE it. A third possibility (as you are already into SQL) is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select cats('%RRR(',put(dates,date9.),')') into :code separated by ';'
  from datesFormacro;
quit;
&amp;amp;code;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume that what you really want is the sum for that date, not the same total sum for all dates. But that should be easy to fix inside the macro, just add&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where dates="&amp;amp;date"d&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;too the SQL expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to be honest, what is the point of having a dataset for each date? You can just sum for each date in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create tabe summary as select dates as date,sum(value) as sum_value
  from RawTb
  where dates in(select dates from datesFormacro)
  group by date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and get rid of all the macro stuff.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 08:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-for-values-in-data-set/m-p/707378#M217180</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-12-21T08:25:29Z</dc:date>
    </item>
  </channel>
</rss>

