<?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: Find offer amnt and offer model from concatenated fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897464#M354652</link>
    <description>&lt;P&gt;Since the code you show for your TBL1 has values of "offer_amount" of 1000-200&amp;nbsp; 4000-200 and 6000-300 you may need to walk us through more details on where we find values like 2000 (Input variable $ defaults to 8 character length. Your shown values are 9 characters so fix the code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't do us or yourself any favors if we are supposed to look for a "loan model taken" as numeric inside a character variable like Offer_models. If you are not doing arithmetic with the "load model taken" variable it really should not be numeric at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FINDW function can return word order, if found, such as 1, 2 or 3 instead of position of characters using the E option.&lt;/P&gt;
&lt;P&gt;If you are going to look for a numeric value in a character variable then you have to take responsibility for the conversion of the numeric to an appropriate character value for the search.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Oct 2023 19:45:15 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-10-05T19:45:15Z</dc:date>
    <item>
      <title>Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897461#M354649</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp; 2 data sets:&lt;/P&gt;
&lt;P&gt;1- Loans that customer took (Fields-ID(Customer ID),loan model, loan amount&lt;/P&gt;
&lt;P&gt;2-Offers for loans that customers received(Fields-ID (Customer ID),loans models offered, loans amounts offered.As can see these field :loans models offered, loans amounts offered are concatenate of numbers with delimiter '-'.&lt;/P&gt;
&lt;P&gt;I would like to merge the two data sets and find:&lt;/P&gt;
&lt;P&gt;What was the amount offered for the loan customer took&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this I want to calculate 2 fields:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;U&gt;New field 1&lt;/U&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Location of value of&amp;nbsp;loan model(data set 1)&amp;nbsp; in field&amp;nbsp;loans models offered(data set 2).&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For ID=1 loan models offered are "&lt;CODE class=" language-sas"&gt;987-712"&amp;nbsp;&amp;nbsp;and&amp;nbsp;loan&amp;nbsp;model&amp;nbsp;taken&amp;nbsp;is&amp;nbsp;&amp;nbsp;712&amp;nbsp;so&amp;nbsp;the&amp;nbsp;value&amp;nbsp;of&amp;nbsp;the&amp;nbsp;new&amp;nbsp;field&amp;nbsp;should&amp;nbsp;be&amp;nbsp;2&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;For ID=2 loan models offered are is '888' and loan model taken is '888' so the value of new field should be 1&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;For ID=3 loan models offered are is '666' and loan model taken is '222' so the value of new field should be 0&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;U&gt;New field 2&lt;/U&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;Based&amp;nbsp;on&amp;nbsp;value&amp;nbsp;of&amp;nbsp;new&amp;nbsp;field&amp;nbsp;1&amp;nbsp;I&amp;nbsp;want&amp;nbsp;to&amp;nbsp;find&amp;nbsp;the&amp;nbsp;specific&amp;nbsp;offer&amp;nbsp;amount.&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;For example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;For&amp;nbsp;ID=1&amp;nbsp;offer&amp;nbsp;is&amp;nbsp;2000&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;For&amp;nbsp;ID=2&amp;nbsp;offer&amp;nbsp;is&amp;nbsp;&amp;nbsp;5000&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;For&amp;nbsp;ID=2&amp;nbsp;offer&amp;nbsp;is&amp;nbsp;&amp;nbsp;NULL&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;What&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;calculated&amp;nbsp;the&amp;nbsp;two&amp;nbsp;desired&amp;nbsp;fields&amp;nbsp;please?&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl1;
infile datalines delimiter=',';
informat offer_amounts $200. offer_models $200.;
Input ID offer_amounts $ offer_models $;
cards;
1,1000-2000,987-712
2,5000,888
3,3000,666
4,4000-2000,333-421
5,6000-3000,555-431
6,9000,777
;
Run;


Data tbl2;
input ID loan_Model loan_amount;
cards;
1 712 1700
2 888 5000
3 222 8000
4 333 4000
5 431 5000
6 777 2000
;
Run;


proc sql;
create table want as
select a.*,b.offer_amounts,b.offer_models
from tbl2 as a
left join tbl1 as b
on a.ID=b.ID
;
quit;
/***step1-Search loan_Model in field offer_models and find its position***/
/**step2-Find the relevant offer based on the position found in step1**/
/*ID=1, position 2---- offer 2000*/
/*ID=2, position 1-----offer 5000*/
/*ID=3, position 0-----offer Null*/
/*ID=4, position 1-----offer 4000*/
/*ID=5, position 2-----offer 5000*/
/*ID=5, position 1-----offer 9000*/

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 20:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897461#M354649</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-05T20:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897464#M354652</link>
      <description>&lt;P&gt;Since the code you show for your TBL1 has values of "offer_amount" of 1000-200&amp;nbsp; 4000-200 and 6000-300 you may need to walk us through more details on where we find values like 2000 (Input variable $ defaults to 8 character length. Your shown values are 9 characters so fix the code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't do us or yourself any favors if we are supposed to look for a "loan model taken" as numeric inside a character variable like Offer_models. If you are not doing arithmetic with the "load model taken" variable it really should not be numeric at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FINDW function can return word order, if found, such as 1, 2 or 3 instead of position of characters using the E option.&lt;/P&gt;
&lt;P&gt;If you are going to look for a numeric value in a character variable then you have to take responsibility for the conversion of the numeric to an appropriate character value for the search.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 19:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897464#M354652</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-05T19:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897465#M354653</link>
      <description>&lt;P&gt;Sorry&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl1;
infile datalines delimiter=',';
informat offer_amounts $200. offer_models $200.;
Input ID offer_amounts $ offer_models $;
cards;
1,1000-2000,987-712
2,5000,888
3,3000,666
4,4000-2000,333-421
5,6000-3000,555-431
6,9000,777
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Oct 2023 19:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897465#M354653</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-05T19:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897466#M354654</link>
      <description>&lt;P&gt;Can you please show code?&lt;/P&gt;
&lt;P&gt;FINDW function provide me the character location but I need word location .&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;Word "712" location in "987-712"&amp;nbsp; should be 2&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then I can get offer "2000 "&amp;nbsp; from "1000-2000"&amp;nbsp; &amp;nbsp;using SCAN function with delimiter '-'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 19:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897466#M354654</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-05T19:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897470#M354656</link>
      <description>&lt;P&gt;There is also problem- when the concatenate value has no delimiter (in case that it have only one word) then the findW&amp;nbsp; with 'E' option doesnt calculate well&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 20:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897470#M354656</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-05T20:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897504#M354661</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is also problem- when the concatenate value has no delimiter (in case that it have only one word) then the findW&amp;nbsp; with 'E' option doesnt calculate well&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Lets see your code and describe what "doesn't calculate well" means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this gives you ideas:&lt;/P&gt;
&lt;PRE&gt;data example;
   merge tbl1 tbl2;
   by id;
   model_position = findw(strip(offer_models),strip(put(loan_model,6. -L)),'-','E');
   if model_position&amp;gt;0 then loan_offer  = scan(offer_amounts,model_position,'- ');
run;&lt;/PRE&gt;
&lt;P&gt;Remember SAS pads values with trailing blanks of character values when longer than the defined length.&lt;/P&gt;
&lt;P&gt;If you don't consider those and only use '-' as the delimiter in FINDW then you need need to STRIP (or old school LEFT(trim(variable)) to make sure there are not leading/trailing blanks in the value that would be used in the comparison. "1000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; " is not equal to "1000".&amp;nbsp; You could likely drop the strip for Offer_models but would need to add a space to the list of delimiters so the trailing blanks are not considered in the position determination. Scan will have a similar problem as well as not liking a position of 0 (or missing) for which value to find.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 23:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897504#M354661</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-05T23:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897581#M354700</link>
      <description>&lt;P&gt;Thanks so much,&lt;/P&gt;
&lt;P&gt;Can you please explain again why is it essential to remove leading/trail blanks from first argument of FINDW?&lt;/P&gt;
&lt;P&gt;For the second argument I understand why need to remove&amp;nbsp; leading/trail blanks because actually it is the value we are looking for.&lt;/P&gt;
&lt;PRE&gt;findw(&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;strip&lt;/STRONG&gt;&lt;/FONT&gt;(offer_models),strip(put(loan_model,6. -L)),'-','E')&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 13:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897581#M354700</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-06T13:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find offer amnt and offer model from concatenated fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897585#M354702</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks so much,&lt;/P&gt;
&lt;P&gt;Can you please explain again why is it essential to remove leading/trail blanks from first argument of FINDW?&lt;/P&gt;
&lt;P&gt;For the second argument I understand why need to remove&amp;nbsp; leading/trail blanks because actually it is the value we are looking for.&lt;/P&gt;
&lt;PRE&gt;findw(&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;strip&lt;/STRONG&gt;&lt;/FONT&gt;(offer_models),strip(put(loan_model,6. -L)),'-','E')&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This issue is the spaces padded onto the end of the variable with the hyphen delimited list.&amp;nbsp; If you look for the word '712' and the string has the value '987-712&amp;nbsp; &amp;nbsp; &amp;nbsp;'&amp;nbsp; then it will not find.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the FINDW() function has a modifier to trim the arguments.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
  length offer_amounts offer_models $200;
  input ID offer_amounts offer_models ;
cards;
1 1000-2000 987-712
2 5000      888
3 3000      666
4 4000-2000 333-421
5 6000-3000 555-431
6 9000      777
;

data tbl2;
  input ID loan_Model loan_amount;
cards;
1 712 1700
2 888 5000
3 222 8000
4 333 4000
5 431 5000
6 777 2000
;

data example;
  merge tbl1 tbl2;
  by id;
  model_position = findw(offer_models,cats(loan_model),'-','Et');
  if model_position&amp;gt;0 then loan_offer  = input(scan(offer_amounts,model_position,'-'),32.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note:&amp;nbsp; CATS() will convert a number to a string without any leading or trailing spaces.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&amp;nbsp; STRIP() will remove leading and trailing spaces.&amp;nbsp; TRIM(LEFT()) can be used as a substitute for STRIP() but LEFT(TRIM()) cannot because that will just move the leading spaces to trailing spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your real problem you should convert TBL1 to have one row per offer.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data offers;
  set tbl1;
  do index=1 to countw(offer_amounts,'-');
    loan_model = input(scan(offer_models,index,'-'),32.);
    loan_offer = input(scan(offer_amounts,index,'-'),32.);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you just need to merge/join on ID and LOAN_MODEL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select * 
from offers natural right join tbl2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 14:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-offer-amnt-and-offer-model-from-concatenated-fields/m-p/897585#M354702</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-06T14:44:28Z</dc:date>
    </item>
  </channel>
</rss>

