<?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 do I change a Macro into a function like macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399118#M96630</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mivcust_acct;
infile cards dlm='09'x;
input custid name:$50. acctid startdate:date9. enddate:date9.;
format startdate enddate date9.;
cards;
1	Test Uno Testesen	10	01JAN2010	01JAN2011
1	Test Uno Testesen	11	01JAN2012	01JAN2014
1	Test Uno Testesen	12	01JAN2013	26MAR2020
2	Test Dos Testesen	20	01JAN2011	01JAN2015
3	Test Tres Testesen	30	01JAN2015	01SEP2017
4	Test Quatro Testesen	40	01JAN2010	08OCT2017
;
run;

data want (keep=custid name earliest_date);
set mivcust_acct;
by custid;
retain earliest_date;
format earliest_date date9.;
if first.custid or startdate &amp;gt; lag(enddate)
then earliest_date = startdate;
if last.custid then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I took the liberty to greatly simplify your example data setup.&lt;/P&gt;</description>
    <pubDate>Wed, 27 Sep 2017 11:16:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-09-27T11:16:02Z</dc:date>
    <item>
      <title>how do I change a Macro into a function like macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399105#M96626</link>
      <description>&lt;P&gt;I am trying to get the earliest start date from a continued series of accounts owned by the same customer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An account ending before any other account started should therefor NOT be counted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The goal is to be able to call the macro as a function from a proc sql like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am on SAS 9.2 in an aix environment (my workstation being an ordinary windows laptop)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Insert into testtable( custID, dateOfEarliestAccount )&lt;/P&gt;&lt;P&gt;values( 3, macro3( 3, &amp;lt;date of today&amp;gt; ) );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below&amp;nbsp;macro demonstrates what I need, but it is not a function style macro. It puts the right answer in the log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For it to be a function I should have the "fourth last line" ("&lt;SPAN&gt;%put &amp;amp;inputDate;") changed into simply "&amp;amp;inputDate" or that's how I have understood it.&amp;nbsp;&lt;/SPAN&gt;It seems that the problem then is the proc sql. But I need that sql it's the core of the proces.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please can anybody guid me in the right direction?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code and testdata etc:&lt;/P&gt;&lt;P&gt;%macro test3(inputCust,inputDate);&lt;BR /&gt;%let earliestStartDate=&amp;amp;sysdate;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select min(startDate)&lt;BR /&gt;into :earliestStartDate separated by ' '&lt;BR /&gt;from mivspace.mivcust_acct&lt;BR /&gt;where custid eq &amp;amp;inputCust&lt;BR /&gt;and startDate le &amp;amp;inputDate&lt;BR /&gt;and endDate ge &amp;amp;inputDate;&lt;BR /&gt;quit;&lt;BR /&gt;%if "&amp;amp;earliestStartDate" = "." %then %do;&lt;BR /&gt;%let earliestStartDate=&amp;amp;sysdate;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if &amp;amp;earliestStartDate ge &amp;amp;inputDate %then&lt;BR /&gt;%put &amp;amp;inputDate;&lt;BR /&gt;%else&lt;BR /&gt;%test3(&amp;amp;inputCust,&amp;amp;earliestStartDate);&lt;/P&gt;&lt;P&gt;%mend test3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can call it like this:&lt;/P&gt;&lt;P&gt;%test3( 1, 21089 )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AS you can tell, I use the base representation of dates to have it work (21089 is september 27th og 2017). That might not be neccessary but it works, and&amp;nbsp;it's ok.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my test data:&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;drop table mivspace.mivcust;&lt;BR /&gt;&lt;BR /&gt;create table mivspace.mivcust(&lt;BR /&gt;custID int,&lt;BR /&gt;name varchar( 100 )&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;insert into mivspace.mivcust( custID, name )&lt;BR /&gt;values( 1, 'Test Uno Testesen' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivcust( custID, name )&lt;BR /&gt;values( 2, 'Test Dos Testesen' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivcust( custID, name )&lt;BR /&gt;values( 3, 'Test Tres Testesen' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivcust( custID, name )&lt;BR /&gt;values( 4, 'Test Quatro Testesen' );&lt;/P&gt;&lt;P&gt;drop table mivspace.mivacct;&lt;/P&gt;&lt;P&gt;create table mivspace.mivacct(&lt;BR /&gt;acctID int,&lt;BR /&gt;custID int,&lt;BR /&gt;saldo numeric,&lt;BR /&gt;startDate date,&lt;BR /&gt;endDate date,&lt;BR /&gt;testfield varchar( 10 )&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 10, 1, 1000, 18263, 18628, 'c1a10' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 11, 1, 11000, 18993, 19724, 'c1a11' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 12, 1, 5000, 19359, 22000, 'c1a12' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 20, 2, 1000, 18628, 20089, 'c2a20' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 30, 3, 1000, 20089, 21063, 'c3a30' );&lt;/P&gt;&lt;P&gt;insert into mivspace.mivacct( acctID, custID, saldo, startDate, endDate, testfield )&lt;BR /&gt;values( 40, 4, 12000, 18263, 21100, 'c4a40' );&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select *&lt;BR /&gt;from mivspace.mivacct;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;drop table mivspace.mivcust_acct;&lt;/P&gt;&lt;P&gt;create table mivspace.mivcust_acct&lt;BR /&gt;as&lt;BR /&gt;select c.custID,&lt;BR /&gt;c.name,&lt;BR /&gt;a.acctID,&lt;BR /&gt;a.startDate,&lt;BR /&gt;a.enddate&lt;BR /&gt;from mivspace.mivcust c&lt;BR /&gt;join mivspace.mivacct a&lt;BR /&gt;on c.custID = a.custID&lt;BR /&gt;order by c.custID, a.startDate;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 10:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399105#M96626</guid>
      <dc:creator>MikkelVejlby</dc:creator>
      <dc:date>2017-09-27T10:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: how do I change a Macro into a function like macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399118#M96630</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mivcust_acct;
infile cards dlm='09'x;
input custid name:$50. acctid startdate:date9. enddate:date9.;
format startdate enddate date9.;
cards;
1	Test Uno Testesen	10	01JAN2010	01JAN2011
1	Test Uno Testesen	11	01JAN2012	01JAN2014
1	Test Uno Testesen	12	01JAN2013	26MAR2020
2	Test Dos Testesen	20	01JAN2011	01JAN2015
3	Test Tres Testesen	30	01JAN2015	01SEP2017
4	Test Quatro Testesen	40	01JAN2010	08OCT2017
;
run;

data want (keep=custid name earliest_date);
set mivcust_acct;
by custid;
retain earliest_date;
format earliest_date date9.;
if first.custid or startdate &amp;gt; lag(enddate)
then earliest_date = startdate;
if last.custid then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I took the liberty to greatly simplify your example data setup.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 11:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399118#M96630</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-27T11:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: how do I change a Macro into a function like macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399120#M96632</link>
      <description>&lt;P&gt;Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 11:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399120#M96632</guid>
      <dc:creator>MikkelVejlby</dc:creator>
      <dc:date>2017-09-27T11:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: how do I change a Macro into a function like macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399198#M96670</link>
      <description>&lt;P&gt;Depending on how you intend to use a "function like macro" you probably can't use any Proc or Data step inside the macro. Remember that what a macro does is generate SAS code. So when you attempt somethingl like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;value = %somemacro(a,b,c);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the "macro" calls proc sql then it attempts to resolve when compliling the data step to&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value =&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select a&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;from b&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where c;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;
&lt;P&gt;or similar. So when the compiler sees the resolved Proc sql bit of code you get numerous errors about value = proc sql; is invalid syntax, select is incorrect (for a data step select) and quit is unrecognized.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 14:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-change-a-Macro-into-a-function-like-macro/m-p/399198#M96670</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-27T14:48:01Z</dc:date>
    </item>
  </channel>
</rss>

