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=.;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.