<?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 Lookup value based on multiple variables and between two dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683919#M207191</link>
    <description>&lt;P&gt;Morning all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to know how you would approach the following problem which I am trying to automate:&lt;/P&gt;&lt;P&gt;We have a lookup table in excel that has 4 variables plus a start date and an end date and finally a value to be returned.&amp;nbsp; For example...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd missover;
input county $ town $ school $ class $ start_date :ddmmyy10. end_date :ddmmyy10. Value_To_Return;
format start_date end_date date9.;
cards;
"Essex", "Braintree", "Tabor", "Blue", 12/10/2017, 31/07/2018, 100
"Essex", "Braintree", "Tabor", "Blue", 01/08/2018, 31/12/2018, 150
"Essex", "Braintree", "Tabor", "Blue", 01/01/2019, 15/04/2019, 125
"Essex", "Braintree", "Tabor", "Blue", 16/04/2019, 31/12/2099, 140
"Essex", "Braintree", "Tabor", "Red", 12/10/2017, 31/07/2018, 100
"Essex", "Braintree", "Tabor", "Red", 01/08/2018, 31/12/2018, 140
"Essex", "Braintree", "Tabor", "Red", 01/01/2019, 15/04/2019, 130
"Essex", "Braintree", "Tabor", "Red", 16/04/2019, 31/12/2099, 140
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Want&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I want to get the Value_To_Return field from any dataset that has the first 4 variables to match on, plus a variable called Event_Date which should be used as the data look up for between Start_Date and End_Date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a multi variable Format Lookup I could use?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obviously the example above only has 8 rows, but the reality is many thousands.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Sep 2020 11:04:08 GMT</pubDate>
    <dc:creator>Jamie_H</dc:creator>
    <dc:date>2020-09-15T11:04:08Z</dc:date>
    <item>
      <title>Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683919#M207191</link>
      <description>&lt;P&gt;Morning all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to know how you would approach the following problem which I am trying to automate:&lt;/P&gt;&lt;P&gt;We have a lookup table in excel that has 4 variables plus a start date and an end date and finally a value to be returned.&amp;nbsp; For example...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd missover;
input county $ town $ school $ class $ start_date :ddmmyy10. end_date :ddmmyy10. Value_To_Return;
format start_date end_date date9.;
cards;
"Essex", "Braintree", "Tabor", "Blue", 12/10/2017, 31/07/2018, 100
"Essex", "Braintree", "Tabor", "Blue", 01/08/2018, 31/12/2018, 150
"Essex", "Braintree", "Tabor", "Blue", 01/01/2019, 15/04/2019, 125
"Essex", "Braintree", "Tabor", "Blue", 16/04/2019, 31/12/2099, 140
"Essex", "Braintree", "Tabor", "Red", 12/10/2017, 31/07/2018, 100
"Essex", "Braintree", "Tabor", "Red", 01/08/2018, 31/12/2018, 140
"Essex", "Braintree", "Tabor", "Red", 01/01/2019, 15/04/2019, 130
"Essex", "Braintree", "Tabor", "Red", 16/04/2019, 31/12/2099, 140
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Want&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I want to get the Value_To_Return field from any dataset that has the first 4 variables to match on, plus a variable called Event_Date which should be used as the data look up for between Start_Date and End_Date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a multi variable Format Lookup I could use?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obviously the example above only has 8 rows, but the reality is many thousands.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 11:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683919#M207191</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2020-09-15T11:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683922#M207193</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to get the Value_To_Return field from any dataset that has the first 4 variables to match on, plus a variable called Event_Date which should be used as the data look up for between Start_Date and End_Date.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I don't understand this. Explain step-by-step, with an example or two or three. Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 11:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683922#M207193</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-15T11:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683925#M207195</link>
      <description>&lt;P&gt;I have the dataset in the SAS code (albeit it exists in Excel at the moment)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to use any other dataset I have to lookup the final value (Value_To_Return).&amp;nbsp; &amp;nbsp; So if I have another dataset that has:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;County - "Essex", Town - "Braintree", School - "Tabor", Class - "Blue", Event_Date - 01/10/2018&amp;nbsp; &amp;nbsp;Then I would like the value of 150 to be returned.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 11:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683925#M207195</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2020-09-15T11:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683933#M207199</link>
      <description>&lt;P&gt;Okay, thank you, I think I understand. This seems to be an SQL join between the first table and the second table, by date range. Assuming your dates are actual SAS date values and not character strings, then this ought to work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select h.*,l.event_date from
    have as h left join lookup as l 
    on h.county=l.county and h.town=l.town and h.school=l.school
        and h.class=l.class and l.event_date&amp;gt;=h.start_date and l.event_date&amp;lt;=h.end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Sep 2020 11:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683933#M207199</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-15T11:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683980#M207209</link>
      <description>Thank you.&lt;BR /&gt;&lt;BR /&gt;Do you know if there is a way of implementing the lookup from within a Data Step rather than having the table joining code separately. Ie, from within existing code so you get the variable added to the dataset you are already working with.. as in, you call a format or function you've created to return the Value_To_Return variable. eg;&lt;BR /&gt;&lt;BR /&gt;Data blah;&lt;BR /&gt;Set blahblah;&lt;BR /&gt;x = Put(record, Lookup_Format.);&lt;BR /&gt;or...&lt;BR /&gt;x = Call Get_Value_To_Return(County, Town, School, Class, Event_Date);&lt;BR /&gt;&lt;BR /&gt;?</description>
      <pubDate>Tue, 15 Sep 2020 15:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/683980#M207209</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2020-09-15T15:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684013#M207230</link>
      <description>&lt;P&gt;You could do this in a DATA step with the MERGE command.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 17:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684013#M207230</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-15T17:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684140#M207293</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, if you can do it in SQL you can also do it as a merge, but i'm specifically after a method to do it as a lookup from within any datastep as per my request.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 06:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684140#M207293</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2020-09-16T06:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684162#M207300</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    merge have lookup;
    by county town school class;
    if start_date&amp;lt;=event_date&amp;lt;=end_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes both data sets are sorted by COUNTY TOWN SCHOOL CLASS.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 10:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684162#M207300</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-16T10:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Lookup value based on multiple variables and between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684164#M207302</link>
      <description>&lt;P&gt;You're not getting what I mean by lookup within a datastep Paige...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Lookup;
infile datalines dsd missover;
input county $ town $ school $ class $ start_date :ddmmyy10. end_date :ddmmyy10. Value_To_Return;
format start_date end_date date9.;
cards;
"Essex", "Braintree", "Tabor", "Blue", 12/10/2017, 31/07/2018, 100
"Essex", "Braintree", "Tabor", "Blue", 01/08/2018, 31/12/2018, 150
"Essex", "Braintree", "Tabor", "Blue", 01/01/2019, 15/04/2019, 125
"Essex", "Braintree", "Tabor", "Blue", 16/04/2019, 31/12/2099, 140
"Essex", "Braintree", "Tabor", "Red", 12/10/2017, 31/07/2018, 120
"Essex", "Braintree", "Tabor", "Red", 01/08/2018, 31/12/2018, 140
"Essex", "Braintree", "Tabor", "Red", 01/01/2019, 15/04/2019, 130
"Essex", "Braintree", "Tabor", "Red", 16/04/2019, 31/12/2099, 140
;
run;


Data Have;
infile datalines dsd missover;
input county $ town $ school $ class $ eventdate :ddmmyy10.;
format eventdate  date9.;
cards;
"Essex", "Braintree", "Tabor", "Blue", 15/3/2019
"Essex", "Braintree", "Tabor", "Blue", 01/10/2018
"Essex", "Braintree", "Tabor", "Red", 12/01/2019
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For this example, I would want to be working with the Have dataset and get the Value_To_Return where all 4 character variables match and also where eventdate was &amp;lt;= start_date and &amp;lt;= end_date.&lt;BR /&gt;The data in these instances would return 125 on row 1, 150 on row 2 and 130 on row 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have written the Proc FCMP that looks up the character variables, but I dont know how to adapt it to include the date range lookup as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.functions.samples;;
function get_score(county $, town $, school $, class $);
declare hash Lkup(dataset: "work.Lookup");
rc = lkup.definedata("Value_To_Return");
rc=lkup.definekey("county", "town", "school", "class");
 rc=lkup.definedone();
 rc=lkup.find();
 if rc eq 0 then return(Value_To_Return);
 else return(.);
 endsub;
quit;
options cmplib=work.functions;  

data want;
set have;
returned_value = get_score(county, town, school, class);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any suggestions on how to include a clause within the hash lookup to search for eventdate being between start_date and end_date?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 10:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lookup-value-based-on-multiple-variables-and-between-two-dates/m-p/684164#M207302</guid>
      <dc:creator>Jamie_H</dc:creator>
      <dc:date>2020-09-16T10:48:14Z</dc:date>
    </item>
  </channel>
</rss>

