<?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 create a function in PROC FCMP using information from a dataset? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/742012#M80563</link>
    <description>&lt;P&gt;To answer the original question of how to do this in PROC FCMP, I borrowed the data sets from ballardw and the hash code from andreas_lds. The hash code in this SAS program is a little different because in FCMP that hash object has some differences from the DATA step hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input_1;
   input Var1 $	Var2	Date1 :mmddyy10.	Date2  :mmddyy10.	Date3  :mmddyy10. ;
   format date1 date2 date3 mmddyy10.;
   select (var1);
      when('A') targetdate=date1;
      when('B') targetdate=date2;
      when('C') targetdate=date3;
   end;
datalines;
A	1	1/1/2019	1/16/2020	1/30/2021
B	2	6/15/2018	6/30/2019	7/14/2020
C	2	5/26/2018	6/10/2019	6/24/2020
;
run;

data input_2;
  input Var2	Year	Var3;
datalines;
1	2018	1000
1	2019	2000
1	2020	3000
1	2021	4000
2	2018	5000
2	2019	6000
2	2020	7000
2	2021	8000
;
run;

OPTION CMPLIB=sasuser.funcs;

proc fcmp OUTLIB=sasuser.funcs.example;
   function getVar3(var2, targetDate);
      declare hash h(dataset: 'work.input_2');
	  rc = h.defineKey('var2', 'year');
      rc = h.defineData('var3');
	  rc = h.defineDone();

      year = YEAR(targetDate);
	  rc = h.find();

      return(var3);
	  endsub;
	quit;

data result(drop=targetdate);
  set input_1;
  var3=getVar3(var2, targetdate);
  run;

proc print data=work.result; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 May 2021 21:07:16 GMT</pubDate>
    <dc:creator>BillM_SAS</dc:creator>
    <dc:date>2021-05-17T21:07:16Z</dc:date>
    <item>
      <title>How to create a function in PROC FCMP using information from a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741590#M80556</link>
      <description>&lt;P&gt;I have a dataset similar to&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date3&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;1/16/2020&lt;/TD&gt;&lt;TD&gt;1/30/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;6/15/2018&lt;/TD&gt;&lt;TD&gt;6/30/2019&lt;/TD&gt;&lt;TD&gt;7/14/2020&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5/26/2018&lt;/TD&gt;&lt;TD&gt;6/10/2019&lt;/TD&gt;&lt;TD&gt;6/24/2020&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;And I need to create a function that assigns a value depending on the values of a second dataset like the following&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Var2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Year&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var3&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2020&lt;/TD&gt;&lt;TD&gt;3000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;5000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;6000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2020&lt;/TD&gt;&lt;TD&gt;7000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;8000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;The main idea is that if Var1 = A then the function assign the value of Var3 according to Var2 and the year of Date1, if Var1 = B then the function assigns the value in Var3 according to Var2 and the year of Date2 and if Var1 = C then the function assign the value in Var3 according to Var2 and the year of Date3. In order to get the following result:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date3&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&lt;FONT color="#339966"&gt;Var3&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;1/16/2020&lt;/TD&gt;&lt;TD&gt;1/30/2021&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&lt;FONT color="#339966"&gt;2000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;6/15/2018&lt;/TD&gt;&lt;TD&gt;6/30/2019&lt;/TD&gt;&lt;TD&gt;7/14/2020&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&lt;FONT color="#339966"&gt;6000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5/26/2018&lt;/TD&gt;&lt;TD&gt;6/10/2019&lt;/TD&gt;&lt;TD&gt;6/24/2020&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;&lt;FONT color="#339966"&gt;7000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;Thanks a lot for your help!&lt;/P&gt;</description>
      <pubDate>Sat, 15 May 2021 00:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741590#M80556</guid>
      <dc:creator>LilianaRomero</dc:creator>
      <dc:date>2021-05-15T00:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function in PROC FCMP using information from a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741599#M80558</link>
      <description>&lt;P&gt;No need for Proc FCMP, and strong suspicion that if you could get FCMP to work the code is messy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is variation on any sort of look up. "Trick" add a variable with the one date you need to the data set.&lt;/P&gt;
&lt;PRE&gt; data one;
   input Var1 $	Var2	Date1 :mmddyy10.	Date2  :mmddyy10.	Date3  :mmddyy10. ;
   format date1 date2 date3 mmddyy10.;
   select (var1);
      when('A') targetdate=date1;
      when('B') targetdate=date2;
      when('C') targetdate=date3;
   end;
datalines;
A	1	1/1/2019	1/16/2020	1/30/2021
B	2	6/15/2018	6/30/2019	7/14/2020
C	2	5/26/2018	6/10/2019	6/24/2020
;

data two;
  input Var2	Year	Var3;
datalines;
1	2018	1000
1	2019	2000
1	2020	3000
1	2021	4000
2	2018	5000
2	2019	6000
2	2020	7000
2	2021	8000
;

proc sql; 
   create table want as
   select one.var1, one.var2, one.date1, one.date2, one.date3
          ,two.var3
   from one
        left join
        two 
        on one.var2=two.var2 and year(one.targetdate)=two.year
  ;
quit; 
&lt;/PRE&gt;
&lt;P&gt;Proc SQL is designed to have all sorts of tools for combining data on complex conditions. The repeated use of "one" and "two" in the Proc SQL step is that identifies the data set so one.var1 means use var1 from set one.&lt;/P&gt;
&lt;P&gt;If your data is in a library other than work you would create an alias to use a single identifier for the sets like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;"from sashelp.class as a" and use a.variable to reference those as the Proc won't allow use of "sashelp.class.variable".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note how the example data is presented as a data step. That is the preferred way to provide data so we can test code. Test the data step before submitting then on the forum open a text box using the &amp;lt;/&amp;gt; and paste the code. The text box is important because the main message windows on this forum will reformat pasted text often changing data step code so that it may not run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach that might work would be to use code similar to the Select in Data one to create a year variable and then sort and merge the two sets on Var2 and year. However that is more likely to have issues in some cases.&lt;/P&gt;</description>
      <pubDate>Sat, 15 May 2021 05:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741599#M80558</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-15T05:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function in PROC FCMP using information from a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741608#M80559</link>
      <description>&lt;P&gt;You could use a hash-object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
    set work.one;
    
    if 0 then set work.two;
    
    if _n_ = 1 then do;
        declare hash h(dataset: 'work.two');
        h.defineKey('Var2', 'Year');
        h.defineData('Var3');
        h.defineDone();
    end;
    
    select (Var1);
        when ('A') year = year(date1);
        when ('B') year = year(date2);
        when ('C') year = year(date3);
    end;
    
    h.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 May 2021 08:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/741608#M80559</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-15T08:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function in PROC FCMP using information from a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/742012#M80563</link>
      <description>&lt;P&gt;To answer the original question of how to do this in PROC FCMP, I borrowed the data sets from ballardw and the hash code from andreas_lds. The hash code in this SAS program is a little different because in FCMP that hash object has some differences from the DATA step hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input_1;
   input Var1 $	Var2	Date1 :mmddyy10.	Date2  :mmddyy10.	Date3  :mmddyy10. ;
   format date1 date2 date3 mmddyy10.;
   select (var1);
      when('A') targetdate=date1;
      when('B') targetdate=date2;
      when('C') targetdate=date3;
   end;
datalines;
A	1	1/1/2019	1/16/2020	1/30/2021
B	2	6/15/2018	6/30/2019	7/14/2020
C	2	5/26/2018	6/10/2019	6/24/2020
;
run;

data input_2;
  input Var2	Year	Var3;
datalines;
1	2018	1000
1	2019	2000
1	2020	3000
1	2021	4000
2	2018	5000
2	2019	6000
2	2020	7000
2	2021	8000
;
run;

OPTION CMPLIB=sasuser.funcs;

proc fcmp OUTLIB=sasuser.funcs.example;
   function getVar3(var2, targetDate);
      declare hash h(dataset: 'work.input_2');
	  rc = h.defineKey('var2', 'year');
      rc = h.defineData('var3');
	  rc = h.defineDone();

      year = YEAR(targetDate);
	  rc = h.find();

      return(var3);
	  endsub;
	quit;

data result(drop=targetdate);
  set input_1;
  var3=getVar3(var2, targetdate);
  run;

proc print data=work.result; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 May 2021 21:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/742012#M80563</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2021-05-17T21:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function in PROC FCMP using information from a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/742276#M80572</link>
      <description>Thanks, this works perfectly</description>
      <pubDate>Tue, 18 May 2021 20:47:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-function-in-PROC-FCMP-using-information-from-a/m-p/742276#M80572</guid>
      <dc:creator>LilianaRomero</dc:creator>
      <dc:date>2021-05-18T20:47:25Z</dc:date>
    </item>
  </channel>
</rss>

