Hi
Appreciate your advice on my following query
Registration_Code | Country |
2219APPI083993 | UK |
222ABC90839859 | UK |
2219APPI083994 | UK |
222ABC90839861 | HK |
2219APPI083995 | UK |
222ABC90839863 | US |
222ABC90839864 | UK |
222EFG90839865 | UK |
222ABC90839866 | US |
222ABC90839867 | UK |
222ABC90839868 | UK |
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
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,
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,
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;
Best,
Appreciate your advice .
Can you explain the formula below so that I can apply it on my case
prxchange('s/(.*ABC).*/$1/',1
Cheers
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,
You don't need DO in this statement
if find(Registration_code,"ABC")>0 then County ='UK' ;
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;
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
;
One function call:
Data Registration_Code;
set Registration_Code;
country = ifc(find(Registration_code,"ABC"),'UK',country);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.