BookmarkSubscribeRSS Feed
bej
Calcite | Level 5 bej
Calcite | Level 5

Sorry, I am pretty new to SAS and I am not sure how to explain my problem very well. I have a phone number variable and I have extracted the area code out. My state has three area codes, but we have responses from phone numbers all over the US. So, I would like to label the three area codes from my state 1, 2, and 3 from the value below.

 

When I get to the if then statement, I would like to write a line that would basically state: If the area code does not equal (123, 456, 789) code as value 4 (Out of State). Could someone please help me with this? Thanks!

 

value areacode 1='123'
2='456'
3='789'
4='Out of State';

 

if 123 = PhoneNumber then areacode = 1;
else if 456 = PhoneNumber then areacode = 2;
else if 789 = PhoneNumber then areacode = 3;
else areacode = 4;
else areacode=.;

1 REPLY 1
PaigeMiller
Diamond | Level 26

Why even bother formatting a meaningful value like an area code to a non-meaningful number like 1 or 2 or 3? This is not necessary and not helpful to your coding or to the usage of the results.

 

How about this:

 

data two;
    set one;
    if phonenumber not in (123 456 789) then areacode=.;
run;

 

Now, the area codes remain understandable, rather than the meaningless 1 or 2 or 3. You can even assign a format to change the missing to "Out of State".

 

Which brings me to a question ... do you really want to do this logic on variable PHONENUMBER or should you be doing this logic on variable AREACODE? Have you mixed up the variable names? Seems to me you've got the wrong variable, you want to do this on AREACODE and not PHONENUMBER ... or at least you have the wrong variable name given the values it contains.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 280 views
  • 0 likes
  • 2 in conversation