<?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 using an existing dataset (variable is also a data set) in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505900#M1228</link>
    <description>&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the following code to test it out, but I'm getting a few errors:&lt;/P&gt;&lt;P&gt;That I do not understand.&amp;nbsp; Can you perhaps help? (It doesn't use any data that you would need).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro similarity(act_branch,inp_branch);
	min(250,spedis(strip(lowcase(act_branch)),inp_branch)) as cost1,
	min(250,spedis(inp_branch,strip(lowcase(act_branch)))) as cost2,
	max(caculated cost1,caculated cost2) as cost3,
	ifn(caculated cost3 &amp;gt; 0,(1 - (min(calcualted cost1,calculated cost2)/250)),1) as similarity,
	case when calculated similarity &amp;gt; 0.9 then true else false end as flag
%mend similarity;

data test;
similarity = %similarity('Maerua Mall','marua mall');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 19 Oct 2018 12:38:26 GMT</pubDate>
    <dc:creator>BayleyVos</dc:creator>
    <dc:date>2018-10-19T12:38:26Z</dc:date>
    <item>
      <title>How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505871#M1224</link>
      <description>&lt;P&gt;I'm calculating the similarity between two text values (a text response and a standard dataset of responses).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, the text responses could be ('palms','east rand mall','sandton city mall').&amp;nbsp; The standard dataset is ('Palm Centre', 'East Rand Mall','Sandton City',etc...).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I'm calculating the similarity between the two.&amp;nbsp; Given a certain similarity criteria, it should output the value from the standard dataset).&amp;nbsp; Just to clarity the text responses can be anything that someone can text - open text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm calculating the similarity factor as follows:&lt;/P&gt;&lt;P&gt;---------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_branch_list (keep = branch);
set branch_list;

cost1 = min(250,spedis(strip(lowcase(branch)),'east rand mall'));
cost2 = min(250,spedis('east rand mall',strip(lowcase(branch))));
cost3 = max(cost1,cost2);

if cost3 &amp;gt; 0 then similarity = (1 - (min(cost1,cost2)/250));
else similarity = 1;

if similarity &amp;gt; 0.9 then output;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;---------------------------------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The branch_list mentioned in the code is the standard set of inputs.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In this example I've used the text input of 'east rand mall'.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How do I turn this into a function, for which I can input a data set column (with the text values) and then it calculates the correct standard dataset value, based on the similarity factor criteria?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would ideally use this in a proc sql step, by calling the function.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(I'm using SAS version 7.13 HF4 (64-bit))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 09:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505871#M1224</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-19T09:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505877#M1225</link>
      <description>&lt;P&gt;Personally I would avoid "functions".&amp;nbsp; You can macro-tise that code pretty easily.&amp;nbsp; You could also change it so that it works in SQL, however that is different syntax to the one presented below, your macro call needs to generate valid SQL syntax.&amp;nbsp; So something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro similarity;
  min(250,spedis(strip(lowcase(branch)),'east rand mall')) as cost1,
  min(250,spedis('east rand mall',strip(lowcase(branch)))) as cost2,
  max(caculated cost1,caculated cost2) as cost3,
  ifn(caculated cost3 &amp;gt; 0,(1 - (min(calcualted cost1,calculated cost2)/250)),1) as similarity,
  case when calculated similarity &amp;gt; 0.9 then "Y" else "" end as flag
%mend similarity;

proc sql;
  select %similarity
  from...;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Oct 2018 09:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505877#M1225</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-19T09:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505888#M1226</link>
      <description>&lt;P&gt;Thank you so much...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But how do I work in the data set that is needed for the calculations?&amp;nbsp; Because I do not see it mentioned in the macro.&lt;/P&gt;&lt;P&gt;That is the 'branch-list' table that I mentioned in my original post?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or do I not need to specify it?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 10:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505888#M1226</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-19T10:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505898#M1227</link>
      <description>&lt;P&gt;You would need to supply test data in the form of a datastep:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What you want out from it, and code you want to use the macro in.&amp;nbsp; I can't tell you from the post you have made, that is why I just gave you an example of how to use macro to create code which would work in SQL.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 12:30:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505898#M1227</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-19T12:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505900#M1228</link>
      <description>&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the following code to test it out, but I'm getting a few errors:&lt;/P&gt;&lt;P&gt;That I do not understand.&amp;nbsp; Can you perhaps help? (It doesn't use any data that you would need).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro similarity(act_branch,inp_branch);
	min(250,spedis(strip(lowcase(act_branch)),inp_branch)) as cost1,
	min(250,spedis(inp_branch,strip(lowcase(act_branch)))) as cost2,
	max(caculated cost1,caculated cost2) as cost3,
	ifn(caculated cost3 &amp;gt; 0,(1 - (min(calcualted cost1,calculated cost2)/250)),1) as similarity,
	case when calculated similarity &amp;gt; 0.9 then true else false end as flag
%mend similarity;

data test;
similarity = %similarity('Maerua Mall','marua mall');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Oct 2018 12:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505900#M1228</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-19T12:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505908#M1229</link>
      <description>&lt;P&gt;I see where the confusion arises.&amp;nbsp; The macro code I presented was in answer to;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;I would ideally use this in a proc sql step"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Hence why I presented it using an example proc sql step:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;%macro similarity;
  min(250,spedis(strip(lowcase(branch)),'east rand mall')) as cost1,
  min(250,spedis('east rand mall',strip(lowcase(branch)))) as cost2,
  max(caculated cost1,caculated cost2) as cost3,
  ifn(caculated cost3 &amp;gt; 0,(1 - (min(calcualted cost1,calculated cost2)/250)),1) as similarity,
  case when calculated similarity &amp;gt; 0.9 then "Y" else "" end as flag
%mend similarity;

proc sql;
  select %similarity
  from...;
quit;&lt;/PRE&gt;
&lt;P&gt;I did not present a datastep example, for that you would want something like your original code, bu macrotised.&amp;nbsp; Maybe:&lt;/P&gt;
&lt;PRE&gt;%macro similarity (out=,text=);
  data &amp;amp;out. (keep = branch);
    set branch_list;
    cost1 = min(250,spedis(strip(lowcase(branch)),"&amp;amp;text."));
    cost2 = min(250,spedis("&amp;amp;text.",strip(lowcase(branch))));
    cost3 = max(cost1,cost2);
    if cost3 &amp;gt; 0 then similarity = (1 - (min(cost1,cost2)/250));
    else similarity = 1;
    if similarity &amp;gt; 0.9 then output;
  run;
%mend similarity;

%similarity (out=test1,text=east rand mall);
%similarity (out=test2,text=marua mall);
...&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Datastep and SQL syntax's are very different, so what works for one doesn't necessarily work for the other, and there are many ways to implement any problem.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2018 13:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/505908#M1229</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-19T13:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506206#M1274</link>
      <description>&lt;P&gt;Have you considered using function COMPLEV instead? It is faster and symmetric. It also provides a limit to the distance that it will evaluate and useful modifiers.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Oct 2018 04:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506206#M1274</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-10-20T04:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506360#M1302</link>
      <description>&lt;P&gt;Yes, sorry.&amp;nbsp; I think I'm not explaining my problem sufficiently well - I'm very new to SAS (so please bear with me for a while).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your input! It has greatly helped me!&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 07:39:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506360#M1302</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-22T07:39:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506363#M1305</link>
      <description>&lt;P&gt;I used this code (you provided - thank you) to construct the macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro similarity (out=,text=);
  data &amp;amp;out. (keep = branch);
    set branch_list;
    cost1 = min(250,spedis(strip(lowcase(branch)),"&amp;amp;text."));
    cost2 = min(250,spedis("&amp;amp;text.",strip(lowcase(branch))));
    cost3 = max(cost1,cost2);
    if cost3 &amp;gt; 0 then similarity = (1 - (min(cost1,cost2)/250));
    else similarity = 1;
    if similarity &amp;gt; 0.9 then output;
  run;
%mend similarity;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, I want to use this macro in the following way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select	*,
		%similarity(test1, a.message) as branch_der
	from	na_pacc_responses as a;&lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So I'm passing the message column from the na_pass_responses table through the similarity macro - to obtain the output.&amp;nbsp; However, it giving me the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR:&amp;nbsp; More positional parameters found than defined.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm really sorry! But I desperately need help!&lt;/P&gt;&lt;P&gt;What am I doing wrong that it doesn't work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for all your help!!!&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 08:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506363#M1305</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-22T08:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506366#M1307</link>
      <description>&lt;P&gt;Sorry, I am not going over it again, please re-read my posts above.&amp;nbsp; SQL syntax is not the same as datastep code.&amp;nbsp; Your macro needs to resolve to syntactically correct code - all macro is is a find replace.&amp;nbsp; Your code resolves to:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;proc sql;&lt;BR /&gt; select *,&lt;BR /&gt; data test1 (keep = branch);&lt;BR /&gt; set branch_list;&lt;BR /&gt; cost1 = min(250,spedis(strip(lowcase(branch)),"&amp;amp;text."));&lt;BR /&gt; cost2 = min(250,spedis("a.message",strip(lowcase(branch))));&lt;BR /&gt; cost3 = max(cost1,cost2);&lt;BR /&gt; if cost3 &amp;gt; 0 then similarity = (1 - (min(cost1,cost2)/250));&lt;BR /&gt; else similarity = 1;&lt;BR /&gt; if similarity &amp;gt; 0.9 then output;&lt;BR /&gt; run;&lt;BR /&gt; as branch_der&lt;BR /&gt; from na_pacc_responses as a;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Which is clearly wrong.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 08:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506366#M1307</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-22T08:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506367#M1308</link>
      <description>&lt;P&gt;I am really sorry.&amp;nbsp; I'm just very new to SAS - so I my understanding is lacking, which I understand must be frustrating to you.&amp;nbsp; I do apologise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you anyway for your time and effort.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 08:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506367#M1308</guid>
      <dc:creator>BayleyVos</dc:creator>
      <dc:date>2018-10-22T08:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a function using an existing dataset (variable is also a data set)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506369#M1310</link>
      <description>&lt;P&gt;No worries.&amp;nbsp; You have the two different examples, one datastep and one sql.&amp;nbsp; Have a go and come back with (start a new thread) with any questions.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 09:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-function-using-an-existing-dataset-variable-is/m-p/506369#M1310</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-22T09:02:25Z</dc:date>
    </item>
  </channel>
</rss>

