<?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: Replacing If then with Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458388#M116325</link>
    <description>&lt;P&gt;It may help if you explain the&amp;nbsp;business problem to give us some context.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I suspect what they're trying to do is automate the groups, pass/fail?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend a direct format for that. You can create formats from a data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value bmi_fmt
low - 18.5 = 'Underweight'
18.5 - 24.9 = 'Normal'
25 - 29.9 = 'Overweight'
30 - high = 'Obese';
run;

 data class;
    	set sashelp.class;
    	length category $20.;
    	bmi = 703*(weight/(height**2));

    	if bmi &amp;lt; 18 then
    		category='Under Weight';
    	else if 18 &amp;lt;= BMI &amp;lt; 25 then
    		category='Normal';
    	else if 25 &amp;lt;= BMI &amp;lt; 30 then
    		category ='Over Weight';
    	else if BMI &amp;gt;=30 then
    		category = 'Obese';
    run;

title 'Results from IF/THEN statements';
proc freq data=class;
table category;
run;

title 'Results from formats';
proc freq data=class;
table bmi;
format bmi bmi_fmt.;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 28 Apr 2018 19:29:55 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-04-28T19:29:55Z</dc:date>
    <item>
      <title>Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458378#M116321</link>
      <description>&lt;P&gt;Did not see any prior posts for anything this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rather than using If then to create a new variable cutoff for PROC FREQ, I would like to use a %LET statement so I can eliminate the data step that creates the cutoff value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA grades;&lt;BR /&gt;do i = 1 to 100;&lt;BR /&gt;id = i;&lt;BR /&gt;grade = int(100*ranuni(123)+1);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA grades_output;&lt;BR /&gt;set grades;&lt;BR /&gt;if grade &amp;gt;= 70 then cutoff = 1;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;if grade &amp;lt;70 then cutoff = 2;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FORMAT;&lt;BR /&gt;value cutofffmt&lt;BR /&gt;1 = 'Passsed'&lt;BR /&gt;2 = 'Failed';&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FREQ DATA = grades_output;&lt;BR /&gt;title 'Grade Distribution When Cutoff = 70';&lt;BR /&gt;format cutoff cutofffmt.;&lt;BR /&gt;tables cutoff / NOCUM;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/******************************************************************************************************&lt;/P&gt;&lt;P&gt;Using %let&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA grades_output_macro;&lt;BR /&gt;set grades;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO Cutoff;&lt;BR /&gt;%if grade &amp;gt;= 70 %then cutoff = 1;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;%if grade &amp;lt; 70 %then cutoff = 2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FORMAT;&lt;BR /&gt;value cutofffmt&lt;BR /&gt;1 = 'Passsed'&lt;BR /&gt;2 = 'Failed';&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FREQ DATA = grades_output_macro;&lt;BR /&gt;title 'Grade Distribution When Cutoff = 70';&lt;BR /&gt;format cutoff cutofffmt.;&lt;BR /&gt;tables &amp;amp;cutoff / NOCUM;&lt;BR /&gt;%END;&lt;/P&gt;&lt;P&gt;%MEND Cutoff;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%Cutoff&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 17:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458378#M116321</guid>
      <dc:creator>RickyS</dc:creator>
      <dc:date>2018-04-28T17:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458379#M116322</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67527"&gt;@RickyS&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Did not see any prior posts for anything this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;rather than using If then to create a new variable cutoff for PROC FREQ, I would like to use a %LET statement so I can eliminate the data step that creates the cutoff value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This won't work since PROC FREQ cannot work on the results of the %LET statement, but it can work on data step variables. (I fail to see even why you want to do this)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the data step works, so why bother introducing the unnecessary complications of macros?&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 17:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458379#M116322</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-28T17:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458380#M116323</link>
      <description>&lt;P&gt;These statements are a big problem.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if grade &amp;gt;= 70 %then cutoff = 1; 
%if grade &amp;lt; 70 %then cutoff = 2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are testing whether the letter g sorts after the digit 7 in the ASCII coding for characters.&lt;/P&gt;
&lt;P&gt;You might be able to do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO Cutoff(dataset,varname,cutoff);
PROC FORMAT;
  value cutofffmt
    &amp;amp;cutoff - high = 'Passed'
    other = 'Failed'
  ;
RUN;

PROC FREQ DATA = &amp;amp;dataset;
  title "&amp;amp;varname Distribution When Cutoff = &amp;amp;cutoff";
  format &amp;amp;varname cutofffmt.;
  tables &amp;amp;varname / NOCUM;
run;

%MEND Cutoff;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then call it with your original dataset without making any new variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%cutoff(dataset=grades,varname=grade,cutoff=70);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 17:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458380#M116323</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-28T17:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458383#M116324</link>
      <description>&lt;P&gt;Appreciate the discussion, put me on a better track, I think this is the solution they are probably looking for.&amp;nbsp; A let to replace coding a changing grade value in 3 places.&amp;nbsp; My best guess.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Cutoff = 70;&lt;/P&gt;&lt;P&gt;DATA grades_output_macro;&lt;BR /&gt;set grades;&lt;BR /&gt;if grade &amp;gt;= &amp;amp;Cutoff then cutoff = 1;&lt;BR /&gt;if grade &amp;lt; &amp;amp;Cutoff then cutoff = 2;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FORMAT;&lt;BR /&gt;value cutofffmt&lt;BR /&gt;1 = 'Passsed'&lt;BR /&gt;2 = 'Failed';&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FREQ DATA = grades_output_macro;&lt;BR /&gt;title 'Grade Distribution When Cutoff = 70';&lt;BR /&gt;format cutoff passedfmt.;&lt;BR /&gt;tables cutoff / NOCUM;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 18:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458383#M116324</guid>
      <dc:creator>RickyS</dc:creator>
      <dc:date>2018-04-28T18:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458388#M116325</link>
      <description>&lt;P&gt;It may help if you explain the&amp;nbsp;business problem to give us some context.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I suspect what they're trying to do is automate the groups, pass/fail?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend a direct format for that. You can create formats from a data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value bmi_fmt
low - 18.5 = 'Underweight'
18.5 - 24.9 = 'Normal'
25 - 29.9 = 'Overweight'
30 - high = 'Obese';
run;

 data class;
    	set sashelp.class;
    	length category $20.;
    	bmi = 703*(weight/(height**2));

    	if bmi &amp;lt; 18 then
    		category='Under Weight';
    	else if 18 &amp;lt;= BMI &amp;lt; 25 then
    		category='Normal';
    	else if 25 &amp;lt;= BMI &amp;lt; 30 then
    		category ='Over Weight';
    	else if BMI &amp;gt;=30 then
    		category = 'Obese';
    run;

title 'Results from IF/THEN statements';
proc freq data=class;
table category;
run;

title 'Results from formats';
proc freq data=class;
table bmi;
format bmi bmi_fmt.;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 28 Apr 2018 19:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458388#M116325</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-28T19:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458394#M116326</link>
      <description>&lt;P&gt;Switch to using double quotes for your TITLE statement and you can reference the macro variable there also.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title "Grade Distribution When Cutoff = &amp;amp;cutoff";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Use the insert code icons in the forum editor when pasting/editing code in the Forum.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 123px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20240i4B473D3236B32DD3/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 19:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458394#M116326</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-28T19:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing If then with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458737#M116440</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67527"&gt;@RickyS&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Appreciate the discussion, put me on a better track,&lt;FONT size="4"&gt;&lt;STRONG&gt; I think this is the solution they are probably looking for&lt;/STRONG&gt;&lt;/FONT&gt;.&amp;nbsp; A let to replace coding a changing grade value in 3 places.&amp;nbsp; My best guess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is this an exercise in a training session or somesuch? Otherwise I wonder why someone would be looking for a specific solution.&lt;/P&gt;
&lt;P&gt;And if it is a training or test exercise then give us all the conditions and requirements.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Apr 2018 16:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-If-then-with-Macro/m-p/458737#M116440</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-30T16:29:52Z</dc:date>
    </item>
  </channel>
</rss>

