BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sas_user409
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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.

sas_user409
Fluorite | Level 6
Thanks so much for your help!
sas_user409
Fluorite | Level 6
I also have a case statement for email column:
case
when
email is not missing
then 1
else 0
end as flag

could you please tell me how would I also include to say when email does not contain '@' then flag it as 0

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 736 views
  • 2 likes
  • 2 in conversation