Help using Base SAS procedures

length issue

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

length issue

Hi,

I have a field Id, if the id is length of provno less than 7 then delete..

data id;

set id;

cards;1

234-A35

1234567-d45

e453768

*123456789

*12345

;

run;

Please let me know.

Thanks


Accepted Solutions
Solution
‎10-24-2011 04:38 PM
PROC Star
Posts: 7,474

length issue

Couldn't you just state it the way you did in your post?  i.e.,

data want;

  set id;

  if length(id) lt 7 then delete;

run;

View solution in original post


All Replies
Solution
‎10-24-2011 04:38 PM
PROC Star
Posts: 7,474

length issue

Couldn't you just state it the way you did in your post?  i.e.,

data want;

  set id;

  if length(id) lt 7 then delete;

run;

Frequent Contributor
Posts: 90

length issue

Thanks Art..I need to clarify one more thing,If

DOES NOT BEGIN WITH A – Z OR 0 -9 OR THE LENGTH OF ID= 1 then delete.

Please let me know.

Thanks

PROC Star
Posts: 7,474

length issue

A couple of questions:

A-Z or a-z?

1 is less than 7 or have you now changed the requirement of length to just be -1?

Frequent Contributor
Posts: 90

length issue

Hi Art,

DO NOT SELECT A ID WHEN THE ID IS EMPTY OR DOES NOT BEGIN WITH A – Z OR 0 -9 OR THE LENGTH OF ID= 1.

This one for other requirement.

Thanks for your help !!

PROC Star
Posts: 7,474

length issue

I think the following does what you want:

data id;

  infile cards truncover;

  informat id $10.;

  input id;

cards;

1234-A35

1234567-d45

   

e453768

1

A123

*123456789

*12345

;

run;

data want;

  set id;

  if not (('A'<=substr(id,1,1)<='Z') or

         ('0'<=substr(id,1,1)<='9'))

         or length(id) le 1 then delete;

run;

Trusted Advisor
Posts: 1,301

length issue

Here is solution with regex...

data foo;

infile cards truncover;

input @1 id $10.;

cards;

1234-A35

1234567-d45

e453768

1

A123

*123456789

*12345

;

run;

data bar;

set foo;

if prxmatch('/^[A-Z0-9]\S.+$/',id)>0;

run;

Will output

1234-A35

1234567-d4

A123

To add additional case of e453768 change expression to '/^[A-Z0-9]\S.+$/i'

Valued Guide
Posts: 765

Re: length issue

Hi ... neat.

Not knowing much about PRX, I do understand the checking of the first character using

[A-Z0-9] but I don't understand how the string checks the length (criteria > 1).  This ID (with an embedded space

as the 2nd character) seems to be an acceptable ID based on the rules (start with A-Z, 0-9 and length > 1) but is not

picked up by the PRXMATCH string.  I guess that would happen with any acceptable 1st character followed by

one or more spaces plus more characters. 

Any way to modify to be a better check of variable length > 1 (could be useful)?  Thanks.

data foo;

infile cards truncover;

input @1 id $10.;

cards;

A 123

;

run;

data bar;

set foo;

if prxmatch('/^[A-Z0-9]\S.+$/',id)>0;

run;

Trusted Advisor
Posts: 1,301

Re: length issue

I will explain the expression '/^[A-Z0-9]\S.+$/'

^[A-Z0-9] the start of the string is a letter A-Z or digit 0-9

\S the second character in the string is not a white space type

.+$ any character 1 or more times till end of string

because we are not striping the padding from the inbound variable this will match any string of more than 1 character even though it seems to imply an excepted length or three or more and not 2 or more.

In order to accept these cases with embeded spaces I would modify the expression to the following.

prxmatch('/^[A-Z0-9].{1,}$/',strip(id))

^[A-Z0-9] the start of the string is letter A-Z or digit 0-9

.{1,}$ followed by any character (except new line) 1 or more times

without the strip function on the inbound variable id the strings will be padded and match for length even though they may not be more that 1 character long.

data foo(where=(prxmatch('/^[A-Z0-9].{1,}$/',strip(id))));

infile cards truncover;

input @1 id $10.;

cards;

1234-A35

1234567-d45

e453768

1

A123

A 123

A     123

*123456789

*12345

;

run;

1234-A35

1234567-d4

A123

A 123

A     123

like previous to make the expression not case sensitive '/^[A-Z0-9].{1,}$/i' will capture the additional id e453768.

This expression is also the same as /^[A-Z0-9].+$/

+ means repeat previous statement 1 or more times

{n} means repeat exactly n times

{n,m} means repeat n to m times

{n,} means repeat n or more times

so + and {1,} are the same...

Super User
Posts: 10,028

length issue

How about:

data id;
  infile cards truncover;
  informat id $10.;
  input id;
cards;
1234-A35
 
1234567-d45
   
e453768
1
A123
*123456789
*12345
;
run;

 

data want;
  set id;
if lengthn(id) in (0 1) or anyalnum(id) ne 1  then delete;
run;

Ksharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 230 views
  • 0 likes
  • 5 in conversation