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;
The SUBSTR Function requires a character argument.
The SUBSTR Function requires a character argument.
Yes that worked as soon as I saw the first comment I realized what it was. Thanks for the help
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.
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 lock in 2025 pricing—just $495!
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.