How to capture special character space in character string

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

How to capture special character space in character string

Hi All,

I want to find special characters for the variable name but  i am missing few records where the variable name contain space followed by alphabetical or space in between alphabets  or alphabets followed by space.

 

How to capture records using regular expression where spaces are available like pid 105, 106 and 109.

I use the below approach.

 

 

 

data find;
input pid name $;
cards;
101 acbd
102 !and
103 X.Y
104 1TVN
105 A BCD
106 bd  
107 ANKR
108 K@234
109  KRS
110 235
;
run;

data find1;
set find;
found= prxchange('s/[A-Za-z.]//i',-1,name);
run;

data find2;
set find1;
where found ne '';
run;

 

Thanks


Accepted Solutions
Solution
‎10-25-2017 08:40 AM
Super User
Super User
Posts: 9,376

Re: How to capture special character space in character string

You just need to fiddle with it a bit:

data have;
infile datalines dlm=",";
input pid name $;
datalines;
102,!and
104,1TVN
108,K@234
105,A BCD
;
run;
data want;
set have;
find=compress(name,"","a");
run;

 This works for all but the space part.  Now this is more of an issue, because depending on the length of name all of the given items contain blanks after the text up to the length of the text string.  So say name is $8:

!and

has four "spaces" after the text there.  So checking space afterwards doesn't make much sense.  If its before or during then possibly.

View solution in original post


All Replies
Super User
Super User
Posts: 9,376

Re: How to capture special character space in character string

Use compress:

data find2;
  set find;
  where lengthn(compress(name,," ","ad"))>0;
run;

This will remove all alphanumeric characters from the string, then if the length left is greater than zero you have special characters.

Contributor
Posts: 63

Re: How to capture special character space in character string

Thanks RW09.

But my query is to find all PID where all special characters including space (i.e space before text, in between text and after text) will be there.

 

My Output would be

 

pid    name   found
102   !and       !
104   1TVN    1
108  K@234 @234
110  235       235
105  A BCD
106 bd
109 KRS

Solution
‎10-25-2017 08:40 AM
Super User
Super User
Posts: 9,376

Re: How to capture special character space in character string

You just need to fiddle with it a bit:

data have;
infile datalines dlm=",";
input pid name $;
datalines;
102,!and
104,1TVN
108,K@234
105,A BCD
;
run;
data want;
set have;
find=compress(name,"","a");
run;

 This works for all but the space part.  Now this is more of an issue, because depending on the length of name all of the given items contain blanks after the text up to the length of the text string.  So say name is $8:

!and

has four "spaces" after the text there.  So checking space afterwards doesn't make much sense.  If its before or during then possibly.

Contributor
Posts: 63

Re: How to capture special character space in character string

Thank you very much for your support.

 

Is there any possibility to capture the same record using prxchange function. I would like to learn it. I browse in google and found /s to be used. But it is not working.

 

Any suggestion will be highly appreciated

Super User
Posts: 9,855

Re: How to capture special character space in character string

[ Edited ]

In your original dataset, the "leading" and "included" spaces never end up in the variables, as they are considered to be delimiters:

data find;
input pid name $;
cards;
101 acbd
102 !and
103 X.Y
104 1TVN
105 A BCD
106 bd  
107 ANKR
108 K@234
109  KRS
110 235
;
run;

data check;
set find;
check = put(name,$hex16.);
run;

proc print data=check noobs;
run;

Result:

pid    name          check

101    acbd     6163626420202020
102    !and     21616E6420202020
103    X.Y      582E592020202020
104    1TVN     3154564E20202020
105    A        4120202020202020
106    bd       6264202020202020
107    ANKR     414E4B5220202020
108    K@234    4B40323334202020
109    KRS      4B52532020202020
110    235      3233352020202020

To read leading and inserted spaces successfully, use the dsd and dlm= options, and the $CHAR informat:

data find;
infile cards dlm=' ' dsd;
input pid name :$char8.;
cards;
101 "acbd"
102 "!and"
103 "X.Y"
104 "1TVN"
105 "A BCD"
106 "bd  "
107 "ANKR"
108 "K@234"
109 " KRS"
110 "235"
;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 63

Re: How to capture special character space in character string

Posted in reply to KurtBremser

Thanks once again for your guidance.

 

I would like to learn is there any option in regular expression where I can add to prxchange function to capture the PID containing space 

like 105 and 109 or any alternative regexpression.

prxchange('s/[A-Za-z.]//i',-1,name);

 

 

Contributor
Posts: 63

Re: How to capture special character space in character string

Can anybody help me is it possible to capture special characters using regular expression including space.
Thanks in advance
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 234 views
  • 1 like
  • 3 in conversation