BookmarkSubscribeRSS Feed
sdevenny
Calcite | Level 5

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

3 REPLIES 3
Kurt_Bremser
Super User

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;
andreas_lds
Jade | Level 19

If you can't find leading blanks, check the spelling: do you have "IOWA" or "Iowa" in the data?

ballardw
Super User

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.

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
  • 3 replies
  • 468 views
  • 0 likes
  • 4 in conversation