<?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: Create a macro that implement this functionality in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632248#M21042</link>
    <description>&lt;P&gt;It's really homework. But I'm sorry I don't have a clue. I'm not familiar with macro at all.&lt;/P&gt;</description>
    <pubDate>Sun, 15 Mar 2020 11:42:12 GMT</pubDate>
    <dc:creator>Moony</dc:creator>
    <dc:date>2020-03-15T11:42:12Z</dc:date>
    <item>
      <title>Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/631545#M20950</link>
      <description>&lt;P&gt;Create a macro that implement this functionality: by specifying a dataset name and a variable name in the dataset, draw a histogram of the variable if the variable is a numerical variable, and draw a bar chart of the variable if the variable is a categorical variable Then use the macro to draw a histogram of age at death as well as a bar chart of cause of death for the SASHELP.Heart dataset. You may want to use the VARTYPE function.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 13:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/631545#M20950</guid>
      <dc:creator>Moony</dc:creator>
      <dc:date>2020-03-12T13:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/631549#M20952</link>
      <description>&lt;P&gt;Since this sounds like homework, let us see what you have already come up with in terms of code.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 13:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/631549#M20952</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T13:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632248#M21042</link>
      <description>&lt;P&gt;It's really homework. But I'm sorry I don't have a clue. I'm not familiar with macro at all.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 11:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632248#M21042</guid>
      <dc:creator>Moony</dc:creator>
      <dc:date>2020-03-15T11:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632249#M21043</link>
      <description>&lt;P&gt;Is your teacher THAT bad? Usually, stuff in homework is covered in lessons first.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 11:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632249#M21043</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-15T11:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632250#M21044</link>
      <description>&lt;P&gt;Ummm...This is not a SAS course. The use of SAS was only introduced in class. But the homework involves this problem, I can't solve it by myself&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 11:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632250#M21044</guid>
      <dc:creator>Moony</dc:creator>
      <dc:date>2020-03-15T11:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632251#M21045</link>
      <description>&lt;P&gt;Then we need to absolutely start with basics: SAS programs work in steps, and you can use your SAS interface (either Enterprise Guide or SAS Studio) to assist you in creating such steps.&lt;/P&gt;
&lt;P&gt;Further, SAS provides a preprocessor to create code dynamically, the macro preprocessor.&lt;/P&gt;
&lt;P&gt;A macro looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac(dataset,variable);
/* code to be created, and macro statements helping in this */
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You already see the basic structure of what you need to achieve in the first line.&lt;/P&gt;
&lt;P&gt;Now, let's see the code we need to implement in there, so we use SAS Studio to create code for the histogram. In SAS Studio (in University Edition), I opened Tasks and Utilties, opened Diagram, and selected Histogram. Then I selected SASHELP.HEART as data source, and AgeAtDeath as analysis variable. SAS Studio then created this code for me:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=SASHELP.HEART;
	histogram AgeAtDeath /;
	yaxis grid;
run;

ods graphics / reset;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If we wrap this into the macro definition from above, we need to replace the relevant parts with our macro parameters:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac(dataset,variable);

ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=&amp;amp;dataset.;
	histogram &amp;amp;variable. /;
	yaxis grid;
run;

ods graphics / reset;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is the macro for a numeric variable; you can create the code for a bar chart in the same way.&lt;/P&gt;
&lt;P&gt;What is left is how to determine the type of the variable in question, and make our two code parts conditional.&lt;/P&gt;
&lt;P&gt;We can use the vartype function in macro code, but this is actually quite tricky:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsid = %sysfunc(open(sashelp.heart,i));
%let num = %sysfunc(varnum(&amp;amp;dsid,ageatdeath));
%let type = %sysfunc(vartype(&amp;amp;dsid,&amp;amp;num));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let us incorporate this into our macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac(dataset,variable);

%local dsid num type;
%let dsid = %sysfunc(open(&amp;amp;dataset.,i));
%let num = %sysfunc(varnum(&amp;amp;dsid.,&amp;amp;variable.));
%let type = %sysfunc(vartype(&amp;amp;dsid.,&amp;amp;num.));

%if &amp;amp;type. = N %then %do;

ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=&amp;amp;dataset.;
	histogram &amp;amp;variable. /;
	yaxis grid;
run;

ods graphics / reset;

%end;
%else %do;
/* insert the bar chart code here, and insert the macro variables where appropriate */
%end;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You then call the macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymac(sashelp.heart,ageatdeath)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you study your resulting macro code, you will see that most of the two codes is similar, and you need only make a single line conditional. I leave this as an exercise.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 12:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632251#M21045</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-15T12:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632255#M21046</link>
      <description>&lt;P&gt;Wow, thank you very much! This has helped me a lot.&lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 13:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632255#M21046</guid>
      <dc:creator>Moony</dc:creator>
      <dc:date>2020-03-15T13:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632256#M21047</link>
      <description>&lt;P&gt;I'm still surprised you were given a task involving the VARTYPE function; such functions were not covered in the first two&amp;nbsp;&lt;EM&gt;weeks&lt;/EM&gt; of SAS Programming training in Heidelberg ...&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 14:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632256#M21047</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-15T14:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro that implement this functionality</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632305#M21057</link>
      <description>&lt;P&gt;Thank you again for answering my questions!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Mar 2020 02:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-macro-that-implement-this-functionality/m-p/632305#M21057</guid>
      <dc:creator>Moony</dc:creator>
      <dc:date>2020-03-16T02:23:39Z</dc:date>
    </item>
  </channel>
</rss>

