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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.