BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASnewbie2
Fluorite | Level 6

Hi 

 

Appreciate your advice on my following query

 

 

Registration_CodeCountry
2219APPI083993UK
222ABC90839859UK
2219APPI083994UK
222ABC90839861HK
2219APPI083995UK
222ABC90839863US
222ABC90839864UK
222EFG90839865UK
222ABC90839866US
222ABC90839867UK
222ABC90839868UK

 

I'd like to search "ABC" in the "Registration_Code" Field and update the correct Country code in the "Country" Field

 

Data Registration_Code;

Modify Registration_Code;

if Registration_code contains "ABC"  then do County ='UK' ;

end;

 

Unfortunately I'm getting following errors message. Thus, what's the right way to code it. 

 

ERROR 388-185: Expecting an arithmetic operator.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 161-185: No matching DO/SELECT statement.

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

Cheers

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @SASnewbie2 

You can use the find() function instead of CONTAINS or LIKE, which are not valid in an IF statement.

Data Registration_Code;
Modify Registration_Code;
if find(Registration_code,"ABC")>0  then do Country ='UK' ;
end;
run;

Best,

View solution in original post

9 REPLIES 9
ed_sas_member
Meteorite | Level 14

Hi @SASnewbie2 

You can use the find() function instead of CONTAINS or LIKE, which are not valid in an IF statement.

Data Registration_Code;
Modify Registration_Code;
if find(Registration_code,"ABC")>0  then do Country ='UK' ;
end;
run;

Best,

SASnewbie2
Fluorite | Level 6
If i'd like to remove all the character or number after the "ABC", how should I code it ?

Fist I have to search from the Registration_Code filed that contains "ABC" and remove all the character or number after the "ABC"
ed_sas_member
Meteorite | Level 14

Hi @SASnewbie2 

Here is a possible approach to achieve this:


Data Registration_Code;
Modify Registration_Code;
if find(Registration_code,"ABC")>0  then do;
	Country ='UK';
	Registration_code = prxchange('s/(.*ABC).*/$1/',1,Registration_code);
end;
run;

Capture d’écran 2020-04-17 à 15.30.49.png

Best,

SASnewbie2
Fluorite | Level 6

Appreciate your advice . 

 

Can you explain the formula below so that I can apply it on my case

prxchange('s/(.*ABC).*/$1/',1

 

Cheers

ed_sas_member
Meteorite | Level 14
prxchange('s/(.*ABC).*/$1/',1,Registration_code)

The syntax is the following:

prxchange( 's/ pattern to find / replacement pattern /', number of times to perform the research, variable)

 

In your case, the variable is Registration_code.

The prxchange() function will look 1 time for the pattern (.*ABC).* in this variable.

If it finds it, it will replace it by the pattern $1.

 

Pattern (.*ABC).* = any character (.) that appears zero, one or more times (*) followed by ABC, followed by any character (.) that appears zero, one or more times (*). So something like xxxxxABCxxxxx

Pattern $1 =it retrieves the first group enclosed in parenthesis in the pattern to find :(.*ABC).*

 

For more information, you can perform some research about Pearl Regular Expressions.

 

Hope this helps!

 

Best,

PaigeMiller
Diamond | Level 26

You don't need DO in this statement

 

if find(Registration_code,"ABC")>0 then County ='UK' ;
--
Paige Miller
Shmuel
Garnet | Level 18

the errors relate to line:

if Registration_code contains "ABC"  then do County ='UK' ; end;

1) you miss ; after do

if Registration_code contains "ABC"  then do; County ='UK' ; end;

2) in case of one statement you don't need do; - end; at all

if Registration_code contains "ABC"  then County ='UK';

3) the country column label is COUNTRY while your code assign value to COUNTY

   (R is missing ?!)

 

4) end a data step by RUN;

Jagadishkatam
Amethyst | Level 16

you can alternative try perl regular expression

 

data have;
input Registration_Code$15.	Country$;
if prxmatch('m/abc/i',Registration_Code) then country='UK';
cards;
2219APPI083993	UK
222ABC90839859	UK
2219APPI083994	UK
222ABC90839861	HK
2219APPI083995	UK
222ABC90839863	US
222ABC90839864	UK
222EFG90839865	UK
222ABC90839866	US
222ABC90839867	UK
222ABC90839868	UK
;

 

 

 

 

Thanks,
Jag

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 4159 views
  • 0 likes
  • 6 in conversation