<?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: Macro Beginner in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633357#M187884</link>
    <description>&lt;P&gt;Listen to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;, especially the first sentence. This was the key for me to understanding the macro facility as a beginner. The entire macro facility is a text replacement function. You aren't writing actual SAS programming in macros. You should already have the program written. Once you start wrapping that in a macro, you need to understand which values are going to change (the macro variables) and the best way to insert them into your already written code. This doesn't help answer your specific question, but as a beginner, I wanted you to know what concept really helped me and made the macro facility really click. But Reeza is also right that a lot of people think they need a macro when they don't. So thinking of it as a text replacement tool helped me understand when I need to use it and when I don't.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Mar 2020 17:16:55 GMT</pubDate>
    <dc:creator>bobpep212</dc:creator>
    <dc:date>2020-03-19T17:16:55Z</dc:date>
    <item>
      <title>Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633338#M187879</link>
      <description>&lt;P&gt;I'm very new to macros and they are pretty confusing at the moment so any help would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Heres what i'm trying to do:&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a footnote, please summarize the number of the films in which the actor was involved, the average popularity scores of these films, and the number of films that received awards.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assigned each actor an id, now I'm just trying to make macros for the number of films, average popularity, and number of awards. I know I need %sysfunc and maybe %eval but I'm not sure how to do it. I attached below what I've tried.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let id=10;&lt;/P&gt;&lt;P&gt;%let films=%sysfunc(sum(films));&lt;/P&gt;&lt;P&gt;%let pop=%sysfunc(avg(pop));&lt;/P&gt;&lt;P&gt;%let awards=%sysfunc(sum(awards));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data=hw3.films3;&lt;/P&gt;&lt;P&gt;where id=&amp;amp;id;&lt;/P&gt;&lt;P&gt;var title;&lt;/P&gt;&lt;P&gt;title1 "Actor Name: &amp;amp;actor10";&lt;/P&gt;&lt;P&gt;footnote "&amp;amp;films &amp;amp;pop &amp;amp;awards";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 16:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633338#M187879</guid>
      <dc:creator>matoma</dc:creator>
      <dc:date>2020-03-19T16:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633341#M187880</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;Macro variables are literal text replacements and one check - is your code valid when you replace it using cut and paste?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc print data=hw3.films3;

where id=&lt;FONT size="5" color="#FF6600"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;;

var title;

title1 "Actor Name: &amp;amp;actor10"; *not shown so no idea what this should be;

footnote "&lt;FONT size="5"&gt;&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;%sysfunc(sum(films))&lt;/FONT&gt; &lt;FONT color="#FF6600"&gt;%sysfunc(avg(pop)) %sysfunc(sum(awards))&lt;/FONT&gt;"&lt;/STRONG&gt;&lt;/FONT&gt;;

run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT size="2" color="#000000"&gt;This is not valid SAS code. You'll want to first calculate these statistics ahead of time and create macro variables with the values you need ahead of time. A general rules is to first have working SAS code then convert it to a macro.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2" color="#000000"&gt;Note that within the PROC PRINT you can use SUM statement to have the totals printed at the bottom anyways. Not sure if it can do averages, but if not, PROC REPORT will do that.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2" color="#000000"&gt;&lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetVersion=9.4&amp;amp;docsetTarget=p0f022tve3mt0tn162itrb0tkjgi.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=proc&amp;amp;docsetVersion=9.4&amp;amp;docsetTarget=p0f022tve3mt0tn162itrb0tkjgi.htm&amp;amp;locale=en&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2" color="#000000"&gt;Here are some macro references for you, that may help.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2" color="#000000"&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank" rel="noopener"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 16:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633341#M187880</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-19T16:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633351#M187882</link>
      <description>&lt;P&gt;This statement does not make sense.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let films=%sysfunc(sum(films));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are telling the macro process (%sysfunc()) to use the SUM() function. The sum function adds the values you pass it. So SUM(1,2,3) is 6.&amp;nbsp; Not only did you only pass it only one value to sum, but the value passed was the string "films" instead of a number.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 16:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633351#M187882</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-19T16:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633357#M187884</link>
      <description>&lt;P&gt;Listen to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;, especially the first sentence. This was the key for me to understanding the macro facility as a beginner. The entire macro facility is a text replacement function. You aren't writing actual SAS programming in macros. You should already have the program written. Once you start wrapping that in a macro, you need to understand which values are going to change (the macro variables) and the best way to insert them into your already written code. This doesn't help answer your specific question, but as a beginner, I wanted you to know what concept really helped me and made the macro facility really click. But Reeza is also right that a lot of people think they need a macro when they don't. So thinking of it as a text replacement tool helped me understand when I need to use it and when I don't.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 17:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633357#M187884</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2020-03-19T17:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633411#M187900</link>
      <description>&lt;P&gt;This looks like homework, which is fair to ask about.&amp;nbsp; But it's hard to give you a helpful answer without knowing what you're being taught.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have a data set of film info. You want to print all the films that actor with ID #10 was in and add a footnote with:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The name of the actor&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;N&lt;SPAN&gt;umber of the films in which the actor was involved&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp;The average popularity scores of these films&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&amp;nbsp;The number of films that received awards&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;First, you need to calculate the above three statistics. That calculation is not done by the macro language, but by a SAS step.&amp;nbsp; Maybe PROC MEANS and output the value to a data set.&amp;nbsp; Maybe PROC SQL.&amp;nbsp; You could do it in a DATA step.&amp;nbsp; It all depends what you've been learning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then once you have the values calculated, you can write the four values to macro variables.&amp;nbsp; Actor Films Pop and Awards.&amp;nbsp; If you have the values in a SAS dataset, you can use CALL SYMPUT to create macro variables from values in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then once you have the values in macro variables, you can use the macro variables in your code, as you have tried:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=hw3.films3;
  where id=&amp;amp;id;
  var title;
  title1 "Actor Name: &amp;amp;actor";
  footnote "&amp;amp;actor was in &amp;amp;films films with popularity rating &amp;amp;pop and received &amp;amp;awards awards";
run;
title; *Clear them;
footnote;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this is an assignment in the macro language, my guess is they might be asking you to write a macro, so your code would be like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ActorReport(id=);&lt;BR /&gt;  %local actor films pop awards;&lt;BR /&gt;
  *compute the statistics for the actor;
  *SAS code here;&lt;BR /&gt;&lt;BR /&gt;  *create macro variables with the actor name, and each statistic;&lt;BR /&gt;  *SAS code here;&lt;BR /&gt;
  *print the report;&lt;BR /&gt;  proc print data=hw3.films3;
    where id=&amp;amp;id;
    var title;
    title1 "Actor Name: &amp;amp;actor";
    footnote "&amp;amp;actor was in &amp;amp;films films with popularity rating &amp;amp;pop and received &amp;amp;awards awards";
  run;
  title; *Clear them;
  footnote;&lt;BR /&gt;%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then you could call the macro like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%ActorReport(id=10)
%ActorReport(id=20)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 19:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633411#M187900</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-03-19T19:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Beginner</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633521#M187932</link>
      <description>&lt;P&gt;The assignments to your macro variables will not work. A %SYSFUNC statement is calculated immediately, like e.g.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let y=4;&lt;BR /&gt;%let x=%sysfunc(sqrt(&amp;amp;y));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But an assignment like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let films=%sysfunc(sum(films));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will not work, as "films" is a text value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will have to calculate the values you want beforehand.&lt;/P&gt;
&lt;P&gt;You can do this using a macro, e.g.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro print_actor(id);
  %local name films pop awards;
  proc sq noprint;
    select name, sum(films),avg(pop),sum(awards) 
    into :name trimmed, :films trimmed, :pop trimmed, :awards trimmed
    from hw3.films3 
    where id=&amp;amp;id;
  quit;
  title "Actor Name: &amp;amp;name";
  footnote "&amp;amp;films &amp;amp;pop &amp;amp;awards";
  proc print data=hw3.films;
    where id=&amp;amp;id;
    var title;
  run;
%mend;

%print_actor(10);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possibility is to create a view with the values you want, and use #BYVAL in your title and footnotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create view actor_sum as select
    id,title,name,sum(films) as films, avg(pop) as pop,sum(awards) as awards
    from hw3.films
    group by id
    order by id;
quit;

Title "Actor Name: #BYVAL(name)";
Footnote "#BYVAL(films) #BYVAL(pop) #BYVAL(awards)";
proc print data=actor_sum;
  by id name films pop awards;
  var title;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assuming that the NAME variable is on the table as well, and that all records with the same ID have the same NAME value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I personally prefer the last solution (if you only want a single ID, just put a WHERE clause on PROC PRINT).&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 10:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Beginner/m-p/633521#M187932</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-03-20T10:09:29Z</dc:date>
    </item>
  </channel>
</rss>

