Hello
I am attempting to apply a new format to my data, but whenever I try and use the format, I get "Unexpected Value" in the output. However, when I change the format name by even one letter - adding one, deleting one, or changing one in the middle - I get the results I am looking for. This is a project for a class which requires a specific format name ($StateCd), otherwise I would accept the results as they are and not be as picky. I thought maybe the fact that it is the same name as the renamed variable, but even when I do not rename the variable, the format still does not work. If anyone has any input on why and how to fix it, I would really appreciate it!
Code:
PROC FORMAT;
VALUE $StateCd
' ' = 'Unknown'
'Ohio' = 'OH'
'Delaware' = 'DE'
'Alaska' = 'AS'
OTHER = 'Unexpected Value';
RUN;
DATA WORK.Contact_Ohio;
SET HypImpt.OhioResidents (RENAME= (Initials= Inits
State=StateCd)
);
KEEP SSN Inits City StateCd ZipCd;
Inits = CATS( SUBSTR(Inits, 1, 1),
SUBSTR(Inits, 3, 1),
SUBSTR(Inits, 4, 1)
);
RUN;
PROC PRINT DATA = WORK.Contact_OH;
FORMAT StateCd $StateCd.;
RUN;
There are no errors or warnings in the log.
Thanks in advance
Could it be that your statecd is not left-aligned (has leading blanks)?
See this for reference:
PROC FORMAT;
VALUE $StateCd
' ' = 'Unknown'
'IOWA' = 'IA'
'MISSISSIPPI' = 'MS'
'UTAH' = 'UT'
OTHER = 'Unexpected Value';
RUN;
data test;
input statecd $char20.;
datalines;
IOWA
IOWA
;
proc print data=test;
format statecd $statecd.;
run;
If you can't find leading blanks, check the spelling: do you have "IOWA" or "Iowa" in the data?
A previous example I explained that your values in the data must match exactly as defined in the format.
So, are all of your values all upper case for statecd? No leading blanks?
And, have you even considered the ZIP functions I mentioned? The previous very limited output you showed results that would not be generated by your format, so it seems that you have actual values of "Unknown" in your data, or similar, and those would get mapped to the OTHER category.
Or it is time to actually share your input data.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.
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!
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.