<?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 Data Logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392631#M94515</link>
    <description>&lt;P&gt;Do you want to make TRT values for only the existing combinations? Or for all possible combinations? &amp;nbsp;Looks like you want the latter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro trtgrp(in,out,varlist);
%local i n sep var ;
%let n=%sysfunc(countw(&amp;amp;varlist));

proc sql ;
  create table list as 
    select *,monotonic() as trt 
    from 
%let sep=;
%do i=&amp;amp;n %to 1 %by -1 ;
  %let var=%scan(&amp;amp;varlist,&amp;amp;i);
  &amp;amp;sep (select distinct &amp;amp;var from &amp;amp;in) x&amp;amp;i
  %let sep=,;
%end;
  ;
  create table &amp;amp;out as 
    select a.*,b.trt
    from &amp;amp;in a, list b
    where 
%let sep=;
%do i=1 %to &amp;amp;n ;
  %let var=%scan(&amp;amp;varlist,&amp;amp;i);
  &amp;amp;sep a.&amp;amp;var = b.&amp;amp;var
  %let sep=and;
%end;
  ;
quit;
%mend trtgrp ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Putting the sub-queries in reverse order will make the variables appear in the LIST dataset in the original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;2056  options mprint;
2057  %trtgrp(in=have,out=want,varlist=trtan sex);
MPRINT(TRTGRP):   proc sql ;
MPRINT(TRTGRP):   create table list as select *,monotonic() as trt from (select distinct sex from have) x2
, (select distinct trtan from have) x1 ;
NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be
      optimized.
NOTE: Table WORK.LIST created, with 6 rows and 3 columns.

MPRINT(TRTGRP):   create table want as select a.*,b.trt from have a, list b where a.trtan = b.trtan and
a.sex = b.sex ;
NOTE: Table WORK.WANT created, with 4 rows and 5 columns.
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 227px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14788iD970C4D5903A4772/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>Fri, 01 Sep 2017 18:37:36 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-09-01T18:37:36Z</dc:date>
    <item>
      <title>Macro Data Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392593#M94499</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;i have the following dataset&lt;/P&gt;
&lt;P&gt;data have ;&lt;BR /&gt; input sub sex $ race $ trtan;&lt;BR /&gt; cards;&lt;BR /&gt; 111 M AA 1&lt;BR /&gt; 112 F AS 2&lt;BR /&gt; 113 F W 3&lt;BR /&gt; 114 M AS 3&lt;BR /&gt; ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;How can i generate the following lines of code based on the categorial variable supplied by the user;&lt;/P&gt;
&lt;P&gt;Two Groups (trtan and sex)&lt;BR /&gt;%macro want(indsn=have , grp=trtan sex);&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;/P&gt;
&lt;P&gt;/*The macro should generate the following lines based the GRP parameters supplied in the macro WANT*/&lt;/P&gt;
&lt;P&gt;if upcase(sex) = "M" and trtan=1 then trt=1;&lt;BR /&gt;if upcase(sex) = "M" and trtan=2 then trt=2;&lt;BR /&gt;if upcase(sex) = "M" and trtan=3 then trt=3;&lt;/P&gt;
&lt;P&gt;if upcase(sex) = "F" and trtan=1 then trt=4;&lt;BR /&gt;if upcase(sex) = "F" and trtan=2 then trt=5;&lt;BR /&gt;if upcase(sex) = "F" and trtan=3 then trt=6;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%macro want(indsn=have , grp=trtan sex race);&lt;/P&gt;
&lt;P&gt;Three Groups (trtan , Race and sex)&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; &lt;BR /&gt;/*The macro should generate the following lines based the GRP parameters supplied in the macro WANT*/&lt;/P&gt;
&lt;P&gt;if upcase(sex) = "M" and trtan=1 and race = "AA" then trt=1;&lt;BR /&gt;if upcase(sex) = "M" and trtan=2 and race = "AS" then trt=2;&lt;BR /&gt;if upcase(sex) = "M" and trtan=3 and race = "W" then trt=3;&lt;/P&gt;
&lt;P&gt;if upcase(sex) = "F" and trtan=1 and race = "AA" then trt=4;&lt;BR /&gt;if upcase(sex) = "F" and trtan=2 and race = "AA" then trt=5;&lt;BR /&gt;if upcase(sex) = "F" and trtan=3 and race = "AA" then trt=6;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 16:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392593#M94499</guid>
      <dc:creator>Almoha</dc:creator>
      <dc:date>2017-09-01T16:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Data Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392613#M94508</link>
      <description>&lt;P&gt;While we can create code that will work for ONLY the variables you show that won't be a very "generic" macro. How would the code generator know that any specific combination gets trt=1?&lt;/P&gt;
&lt;P&gt;For instance we have no idea what to do if you have grp= sex race. And what happens if grp = sex trtan?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm going to assume that&lt;/P&gt;
&lt;P&gt;if upcase(sex) = "F" and trtan=1 and race = "AA" then trt=4;&lt;BR /&gt;if upcase(sex) = "F" and trtan=2 and race = "AA" then trt=5;&lt;BR /&gt;if upcase(sex) = "F" and trtan=3 and race = "AA" then trt=6;&lt;/P&gt;
&lt;P&gt;has a type and you wanted to use "AS" and "W" in there somewhere but if not you need to clarify.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should be able to describe the general rule(s) involved to program something.&lt;/P&gt;
&lt;P&gt;This is not likely to be a short easy to understand solution after you get all of the rules specified.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 17:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392613#M94508</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-01T17:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Data Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392631#M94515</link>
      <description>&lt;P&gt;Do you want to make TRT values for only the existing combinations? Or for all possible combinations? &amp;nbsp;Looks like you want the latter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro trtgrp(in,out,varlist);
%local i n sep var ;
%let n=%sysfunc(countw(&amp;amp;varlist));

proc sql ;
  create table list as 
    select *,monotonic() as trt 
    from 
%let sep=;
%do i=&amp;amp;n %to 1 %by -1 ;
  %let var=%scan(&amp;amp;varlist,&amp;amp;i);
  &amp;amp;sep (select distinct &amp;amp;var from &amp;amp;in) x&amp;amp;i
  %let sep=,;
%end;
  ;
  create table &amp;amp;out as 
    select a.*,b.trt
    from &amp;amp;in a, list b
    where 
%let sep=;
%do i=1 %to &amp;amp;n ;
  %let var=%scan(&amp;amp;varlist,&amp;amp;i);
  &amp;amp;sep a.&amp;amp;var = b.&amp;amp;var
  %let sep=and;
%end;
  ;
quit;
%mend trtgrp ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Putting the sub-queries in reverse order will make the variables appear in the LIST dataset in the original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;2056  options mprint;
2057  %trtgrp(in=have,out=want,varlist=trtan sex);
MPRINT(TRTGRP):   proc sql ;
MPRINT(TRTGRP):   create table list as select *,monotonic() as trt from (select distinct sex from have) x2
, (select distinct trtan from have) x1 ;
NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be
      optimized.
NOTE: Table WORK.LIST created, with 6 rows and 3 columns.

MPRINT(TRTGRP):   create table want as select a.*,b.trt from have a, list b where a.trtan = b.trtan and
a.sex = b.sex ;
NOTE: Table WORK.WANT created, with 4 rows and 5 columns.
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 227px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14788iD970C4D5903A4772/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>Fri, 01 Sep 2017 18:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Data-Logic/m-p/392631#M94515</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-01T18:37:36Z</dc:date>
    </item>
  </channel>
</rss>

