BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Using SAS 9.4 

 

I am having trouble with an else/else if statement. The program will run but in the new column I am creating it does not populate any data. I have checked my POST_CD5 and made sure the data are numeric. Any thoughts on my code would be helpful. Thank you 

 

data work.25Feb ;
set working.19Feb;
format county $char30.;
length COUNTY $100;
IF substr (POST_CD5, 1,5) in (00801) THEN COUNTY = 'Virgin Islands';
else if
substr (POST_CD5, 1,5) in (27105, 27284) THEN COUNTY = 'Forsyth County';
else if
substr (POST_CD5, 1,5) in (28012, 28052, 28098, 28120, 28164) THEN COUNTY = 'Gaston County';
else if
substr (POST_CD5, 1,5) in (28025, 28027, 28081) THEN COUNTY = 'Cabarrus County';
else if
substr (POST_CD5, 1,5) in (28036, 28078, 28105, 28134, 28202, 28205, 28207,
28211, 28213, 28214, 28215, 28216, 28217, 28226,
28227, 28262, 28269, 28270, 28277, 28278) THEN COUNTY = 'Mecklenburg County';
else if
substr (POST_CD5, 1,5) in (28037) THEN COUNTY = 'Lincoln County';
else if
substr (POST_CD5, 1,5) in (28079, 28110, 28173) THEN COUNTY = 'Union County';
else if
substr (POST_CD5, 1,5) in (28086, 28090, 28150) THEN COUNTY = 'Cleveland County';
else if
substr (POST_CD5, 1,5) in (28092) THEN COUNTY = 'Lincoln County';
else if
substr (POST_CD5, 1,5) in (28115) THEN COUNTY = 'Iredell County';
else if
substr (POST_CD5, 1,5) in (28127) THEN COUNTY = 'Stanly County';
else if
substr (POST_CD5, 1,5) in (28139) THEN COUNTY = 'Rutherford County';
else if
substr (POST_CD5, 1,5) in (28146) THEN COUNTY = 'Rowan County';
else if
substr (POST_CD5, 1,5) in (28379) THEN COUNTY = 'Richmond County';
else if
substr (POST_CD5, 1,5) in (28412) THEN COUNTY = 'New Hanover County';
else if
substr (POST_CD5, 1,5) in (28601) THEN COUNTY = 'Catawba County';
else if
substr (POST_CD5, 1,5) in (28604) THEN COUNTY = 'Watauga County';
else if
substr (POST_CD5, 1,5) in (28777) THEN COUNTY = 'Mitchell County';
else if
substr (POST_CD5, 1,5) in (29067) THEN COUNTY = 'Lincoln County';
else if
substr (POST_CD5, 1,5) in (29210) THEN COUNTY = 'Richland County';
else if
substr (POST_CD5, 1,5) in (29340) THEN COUNTY = 'Cherokee County';
else if
substr (POST_CD5, 1,5) in (29707, 29720) THEN COUNTY = 'Lancaster County';
else if
substr (POST_CD5, 1,5) in (29708, 29710, 29715, 29732, 29745) THEN COUNTY = 'York County';
else if
substr (POST_CD5, 1,5) in (29727, 29728) THEN COUNTY = 'Chesterfield County';
else if
substr (POST_CD5, 1,5) in (29909) THEN COUNTY = 'Beaufort County';
ELSE;
RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

The SUBSTR Function requires a character argument.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

The SUBSTR Function requires a character argument.

Astounding
PROC Star
So you checked that POST_CD5 is numeric. So far so good. But SUBSTR works on character strings not numerous. So your code forces SAS to name a numeric to character conversion. If you read the log, you will see many such notes.

If I recall, the latest version of the software contains a SUBSTRN function for numerous. If so, that would be a simple solution.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Yes that worked as soon as I saw the first comment I realized what it was. Thanks for the help

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would advise you change your code slightly to make it more readable, but also more expandable/maintainable.  Your code is effectively stating that the first 5 characters of the string indicate location.  So what we can do in this instance is create a variable, call it location, which holds the first 5 characters - I will keep it as char, but you could use numeric;

proc format;
  value $loc
    "00801"="Virgin Islands"
    "27105","27284"="Forsyth County"
    ...
;
run;

data feb25;
  set feb19;
  location=substr(post_cd5,1,5);
  format location $loc.;
run;

You may even find there is already a dataset or format in sashelp with this information.  The above is far simpler and expandable than endless if statements.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1149 views
  • 0 likes
  • 4 in conversation