How can i remove the data if the variable is not start with a letter. Should i type all the letter or any other short way
variable
A153
B124
0564
.1548
na
w853
I want to keep only these variables. It should be 4 character long
variable
A153
B124
w853
What about 'na'? That starts with a letter two?
Anyway, something like this
data have;
input variable $;
datalines;
A153
B124
0564
.1548
na
w853
;
data want;
set have;
where substr(variable,1,1) not in ('0','1','2','3','4','5','6','7','8','9','.',' ');
run;
Use the length() function to determine the current string length.
Pretty sure your just pulling our leg now, we ask for test data in the form of a datastep every post. As such this is just logic snippets:
data want; string="A153"; isfirstalpha=ifn(lengthn(compress(char(string,1)," ","a")),0,1); islength=lengthn(string); run;
A short way:
if length(variable)=4 and anyalpha(variable)=1;
By using ANYALPHA, you don't have to guess what characters might arrive in the next batch of data.
Just to note its only the first character, so:
if length(variable)=4 and anyalpha(char(variable,1))=1;
Good point about anyalpha() function, saves compress.
No, the complications aren't needed. I stand by my original post.
ANYALPHA returns the position of the first alpha character. So by checking that ANYALPHA returns a 1, that's enough.
data have;
input variable $;
datalines;
A153
B124
0564
.1548
na
w853
;
data want;
set have;
if prxmatch('/^[a-z]\S{3}$/i',strip(variable));
run;
I think@Ksharp meant the following (to ensure that strings were only 4 characters long):
data have; input variable $; datalines; A153 B124xxx d5555 0564 .1548 na AAAA w853 ; data want; set have; if prxmatch('/^[a-z][a-z0-9]{3}$/i',strip(variable)); run;
Art, CEO, AnalystFinder.com
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.