DATA Step, Macro, Functions and more

How to use the SCAN function to finde a string within a string starting with specific characters

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

How to use the SCAN function to finde a string within a string starting with specific characters

 

I have the following data and am trying to use the scan function to find a 10 character string starting with C-

 

PLP C-00201123 042718 OR State Change (1)
X C-00200838 042718 supplies surprise day 5 offer (1)
PLP C-00201124 042718 NJ State Change (1)
X C-00200834 042618 supplies surprise day 4 offer (1)
PTR C-00200836 042618 Encompass Subscribers Credit Card Expiring Soon (1)
PTR C-00200835 Inter-Intra Email 4-26-18 (1)
PTR C-00200837 042618 ELD Roadside Inspections Guide (1)
PTR C-00200953 Encompass Customer Webcast  4-25-18 Reminder (1)
TOD C-00120194 042518 TOD Newsletter - April 2018 (1)
PHR C-00200833 FMLA Invite 4-25-18 (1)
PCS C-00200831 042518 Fall Protection for Construction (1)
PTR C-00200832 042518 Roadcheck 2018 Whitepaper (1)
X C-00200830 042518 supplies surprise day 3 offer (1)
X C-00200827 042418 supplies surprise day 2 offer (1)
PWS C-00200829 KOL Invite (Cintas)  4-24-18 (1)
PWS C-00200828 KOL Invite 4-24-18 (1)
C-00200943 - Free 2290 Poster 04-16-2018 (3)
PTR C-00200524 042318 Encompass Appreciation Offer (1)
X C-00200826 042318 supplies surprise day 1 offer (1)
PTR C-00200954 ELDs and IFTA 4-23-18 (1)
C-00201191 Online Renewal - 1st Notice - sub expiring soon (2)

 

The following code gives me the data in two steps.  I was just wondering if there was another way to write this in one step?  If not,

I can work with this. Thanks!

 

NEW_CODE=scan(Sent_Mailing_Name,'1','C-'-2);
NEW_CODE2=scan(Sent_Mailing_Name,'2','C-'-2);

 

NEW_CODE  NEW_CODE2

PLP                C-00201123
X                     C-00200838
PLP                C-00201124
X                    C-00200834
PTR               C-00200836
PTR               C-00200835
PTR               C-00200837
PTR               C-00200953
TOD              C-00120194
PHR              C-00200833
PCS              C-00200831
PTR                C-00200832
X                      C-00200830
X                      C-00200827
PWS                C-00200829
PWS                C-00200828
C-00200943    -
PTR                 C-00200524
X                     C-00200826
PTR                C-00200954
C-00201191    Online
PTR                C-00201056
X                     C-00201050
C-00201191    Online

 

 

 


Accepted Solutions
Solution
3 weeks ago
Trusted Advisor
Posts: 1,311

Re: How to use the SCAN function to finde a string within a string starting with specific characters

It looks like the first word that begins with "C-" is always your desired result.   If so, then this would work

 

  length new_code $10;
  new_code=substr(sent_mailing_name,index(sent_mailing_name,'C-'));

View solution in original post


All Replies
Solution
3 weeks ago
Trusted Advisor
Posts: 1,311

Re: How to use the SCAN function to finde a string within a string starting with specific characters

It looks like the first word that begins with "C-" is always your desired result.   If so, then this would work

 

  length new_code $10;
  new_code=substr(sent_mailing_name,index(sent_mailing_name,'C-'));
Respected Advisor
Posts: 2,816

Re: How to use the SCAN function to finde a string within a string starting with specific characters

SCAN does not find strings with specific content.

 

Use the FIND function instead.

http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p00ab6ey29t2i8n1ihel88tqtga9.htm&...

--
Paige Miller
Contributor
Posts: 52

Re: How to use the SCAN function to finde a string within a string starting with specific characters

Posted in reply to PaigeMiller

I need to look at FIND a little more.  When I first looked at it, it looks like it returns a location or something and then it would need to be used in other code based on location?  I am used to substr etc.  but does not hurt to learn something new.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 80 views
  • 2 likes
  • 3 in conversation