Sara A.
Sara A.

Reputation: 95

SAS specific observation format

I would like to create a new variable in SAS which takes the value 1 if an observation in the variable "TEXT" contains 8 numbers. The problem is, that TEXT is a character variable. Is it possible to make some kind of a format search in SAS?

Upvotes: 1

Views: 128

Answers (1)

vasja
vasja

Reputation: 4792

I assume by '8 numbers' you actually mean 8 digits. For 8 separate numbers, that would be different. So something like the code below might help. The modifier 'kd' meaning KEEP DIGITS in COMPRESS function does the magic here:

data indata;
length TEXT $20;
input TEXT;
datalines;
a
123
12345678
A12345678
;
run;

data outdata;
set indata;
length TEXT_DIGITS $20 _8_DIGIT_INDICATOR 3;
TEXT_DIGITS = compress(TEXT, , 'kd');
if length(TEXT_DIGITS)=8 then _8_DIGIT_INDICATOR = 1;
run;

Adjust the logic as you need - e.g. if no other character in input value is allowed or something else. Also functions like ANYDIGIT, NOTDIGIT might be useful.

Upvotes: 4

Related Questions