Hi,
I am new to sas EG so unsure how to do this:
I am trying to write a small bit of code within my proc sql that takes a column of phone numbers and creates a new flag column. I want the new column to produce a Y when the phone number is: not empty, 11 digits long, only digits and not equal to '11111111111' and a 'N' otherwise.
Thanks in advance.
In SQL, you need CASE:
proc sql;
create table want as
select
have.*,
case
when
length(have.phone) = 11 and notdigit(compress(have.phone," ")) = 0
and have.phone ne "11111111111"
then 1
else 0
end as flag
from have;
quit;
But it is shorter in a DATA step:
data want;
set have;
if
length(phone) = 11 and notdigit(compress(phone," ")) = 0
and phone ne "11111111111"
then flag = 1;
else flag = 0;
run;
I used numerical 1 and 0 because these can immediately be used as Boolean values later in the code.
In SQL, you need CASE:
proc sql;
create table want as
select
have.*,
case
when
length(have.phone) = 11 and notdigit(compress(have.phone," ")) = 0
and have.phone ne "11111111111"
then 1
else 0
end as flag
from have;
quit;
But it is shorter in a DATA step:
data want;
set have;
if
length(phone) = 11 and notdigit(compress(phone," ")) = 0
and phone ne "11111111111"
then flag = 1;
else flag = 0;
run;
I used numerical 1 and 0 because these can immediately be used as Boolean values later in the code.
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.