<?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: How to output variable values based on condition in macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869917#M343619</link>
    <description>It's hard to comment based on just what you've posted, but I'd guess that you're not using macros at the right step here. I'd suggest first getting it working well for one data set via data step (possibly one variable) and then getting it working for the remainder in iteration. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;</description>
    <pubDate>Fri, 14 Apr 2023 22:02:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-04-14T22:02:14Z</dc:date>
    <item>
      <title>How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869888#M343608</link>
      <description>&lt;P&gt;Hi all ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have sample dataset as below&lt;/P&gt;
&lt;P&gt;In :&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;GRP&lt;/TD&gt;
&lt;TD width="12.5%"&gt;Order&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;n&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;missing&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;min&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="12.5%"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="12.5%"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;7&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want output as below&lt;/P&gt;
&lt;TABLE border="1" width="97.6916030503376%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;GRP&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;order&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;ord&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Stats&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Value&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;A&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;n&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;A&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;min&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;B&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;n(missing)&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;7(2)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;B&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;min&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When missing is 0, no need to present in stats columns, if missing &amp;gt;0 then need to present as n(missing)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried in&amp;nbsp; a macro&amp;nbsp; with below code but not getting expected result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro test;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select MISSING into :m1 - :m2&lt;BR /&gt;from in;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data in2;&lt;BR /&gt;set in;&lt;BR /&gt;by order;&lt;BR /&gt;if &amp;amp;m1 ne 0 then %do;&lt;BR /&gt;stats=" n (missing)";&lt;BR /&gt;value=compress(vvalue(n)||"("||vvalue(MISSING)||")");&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;stats=" n";&lt;BR /&gt;value=compress(vvalue(N));&lt;BR /&gt;%end;&lt;BR /&gt;if &amp;amp;m2 ne 0 %then %do;&lt;BR /&gt;stats=" n (missing)";&lt;BR /&gt;value=compress(vvalue(N)||"("||vvalue(MISSING)||")");&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;stats=" n";&lt;BR /&gt;value=compress(vvalue(N));&lt;BR /&gt;%end;&lt;BR /&gt;output;&lt;BR /&gt;stats=" Min"&amp;nbsp;&lt;BR /&gt;value=compress(vvalue(MIN));&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data in3;&lt;BR /&gt;set in2&lt;BR /&gt;by grp;&lt;BR /&gt;retain ord&lt;BR /&gt;if first.grp then ord=0;&lt;BR /&gt;ord+1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=in3&lt;BR /&gt;by ord order1 stats;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc transpose data=in3 out=want ;&lt;BR /&gt;by ord order1 stats ;&lt;BR /&gt;var value;&lt;BR /&gt;id grp;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I know why it's not working? Appreciate response, thanks&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 19:41:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869888#M343608</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2023-04-14T19:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869898#M343610</link>
      <description>&lt;P&gt;Did you make the data you have by running PROC MEANS?&amp;nbsp; If so, it might be easier to make the data you WANT with PROC MEANS, instead of transposing the output from PROC MEANS.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 20:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869898#M343610</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-14T20:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869909#M343613</link>
      <description>&lt;P&gt;That code has so many bugs...missing semicolons is the biggest, using macro logic when you need data step logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually nothing requires macros so far, not sure why the macro logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the first step you were trying to do cleaned up. This answer is somewhat hardcoded to the data as the KEEP statement includes BP_STATUS which is the grouping variable&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=sashelp.heart nway;
    class bp_status;
    var ageatDeath;
    output out=in n=N NMiss=Missing min=Min;
run;

data in;
set in;
order=1;
*add just to test code;
if _n_ = 1 then missing=0;
run;


%macro test;

    data in2;
        set in;
        by order;

        if missing ne 0 then
            do;
                stats=" n (missing)";
                value=compress(vvalue(n)||"("||vvalue(MISSING)||")");
            end;
        else
            do;
                stats=" n";
                value=compress(vvalue(N));
            end;
        output;
        
        stats=" Min";
        value=compress(vvalue(MIN));
        output;
        keep bp_status order stats value;
        
    run;

  

%mend;

%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, I'd highly recommend checking out the &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-TABLEN/ta-p/634030" target="_self"&gt;TABLEN macro&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80196"&gt;@sri1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have sample dataset as below&lt;/P&gt;
&lt;P&gt;In :&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;GRP&lt;/TD&gt;
&lt;TD width="12.5%"&gt;Order&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;n&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;missing&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;min&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="12.5%"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="12.5%"&gt;1&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;7&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="25%" height="30px"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want output as below&lt;/P&gt;
&lt;TABLE border="1" width="97.6916030503376%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;GRP&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;order&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;ord&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Stats&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Value&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;A&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;n&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;A&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;min&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;B&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;n(missing)&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;7(2)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;B&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;1&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;min&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When missing is 0, no need to present in stats columns, if missing &amp;gt;0 then need to present as n(missing)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried in&amp;nbsp; a macro&amp;nbsp; with below code but not getting expected result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro test;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select MISSING into :m1 - :m2&lt;BR /&gt;from in;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data in2;&lt;BR /&gt;set in;&lt;BR /&gt;by order;&lt;BR /&gt;if &amp;amp;m1 ne 0 then %do;&lt;BR /&gt;stats=" n (missing)";&lt;BR /&gt;value=compress(vvalue(n)||"("||vvalue(MISSING)||")");&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;stats=" n";&lt;BR /&gt;value=compress(vvalue(N));&lt;BR /&gt;%end;&lt;BR /&gt;if &amp;amp;m2 ne 0 %then %do;&lt;BR /&gt;stats=" n (missing)";&lt;BR /&gt;value=compress(vvalue(N)||"("||vvalue(MISSING)||")");&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;stats=" n";&lt;BR /&gt;value=compress(vvalue(N));&lt;BR /&gt;%end;&lt;BR /&gt;output;&lt;BR /&gt;stats=" Min"&amp;nbsp;&lt;BR /&gt;value=compress(vvalue(MIN));&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data in3;&lt;BR /&gt;set in2&lt;BR /&gt;by grp;&lt;BR /&gt;retain ord&lt;BR /&gt;if first.grp then ord=0;&lt;BR /&gt;ord+1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=in3&lt;BR /&gt;by ord order1 stats;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc transpose data=in3 out=want ;&lt;BR /&gt;by ord order1 stats ;&lt;BR /&gt;var value;&lt;BR /&gt;id grp;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I know why it's not working? Appreciate response, thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 21:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869909#M343613</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-14T21:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869913#M343615</link>
      <description>&lt;P&gt;Hi Reeza ,thanks for your response.Missing semicolon is a typo error,thanks for link for TABLEN macro&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 21:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869913#M343615</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2023-04-14T21:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869914#M343616</link>
      <description>&lt;P&gt;Thanks for your response. It's easier with data step but have multiple datasets to work and formatting to fixed decimal places.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 21:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869914#M343616</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2023-04-14T21:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to output variable values based on condition in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869917#M343619</link>
      <description>It's hard to comment based on just what you've posted, but I'd guess that you're not using macros at the right step here. I'd suggest first getting it working well for one data set via data step (possibly one variable) and then getting it working for the remainder in iteration. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;</description>
      <pubDate>Fri, 14 Apr 2023 22:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-variable-values-based-on-condition-in-macro/m-p/869917#M343619</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-14T22:02:14Z</dc:date>
    </item>
  </channel>
</rss>

