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

Hi all,

 

Might be a silly question, non the less I struggle on this one, mostly due to similar coding having worked smoothly in the past. So I am formatting a register data set, and have some struggles converting a character variable into a cathegorical numeric variable. So the data in question is about local regions in Denmark and the coding is looking like the following:

proc format;
	value $MunicipalityRegion	
	"101","147","155","185","165","151","153","157","159","161",
	"163","167","169","183","173","175","187","201","240","210",
	"250","190","270","260","217","219","223","230","400","411" 
	= "Region Capital"
	"253","259","350","265","269","320","376","316","326","360",
	"370","306","329","330","340","336","390" 
	= "Region Zealand"
	"420","430","440","482","410","480","450","461","479","492",
	"530","561","563","607","510","621","540","550","573","575",
	"630","580" 
	= "Region Southern Denmark"
	"710","766","615","707","727","730","741","740","746","706",
	"751","657","661","756","665","760","779","671","791" 
	= "Region Central Denmark"
	"810","813","860","849","825","846","773","840","787","820",
	"851" 
	= "Region Northern Jutland"
	"   " 
	= "Region Missing";
	value $RegionCathegory
	"Region Capital" = 1
	"Region Zealand" = 2
	"Region Southern Denmark" = 3
	"Region Central Denmark" = 4
	"Region Northern Jutland" = 5
	"Region Missing" = 6;
	
run;

....

RegionText = put(Municipality , $MunicipalityRegion.);
	Region = put(RegionText , $RegionCathegory.);

Hence Region should be a 1-6 variable ready to use. The above has been working in the past, but now I get the 

following warning and error:

WARNING: Municipality has already been defined as numeric
Error 48-59: the format REGIONS was not be found or could not be loaded

I'm still a very entry level SAS programmer, and mostly use STATA for coding, hence I might be missing some obvious error in the above. Any suggestions to why regions could not be found or loaded?

 

Best regards,

oggylang

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First thing; when you have any questions about messages, errors or not, you should copy the entire entry for the data step or procedure along with all of the messages. Multiple messages often are related.

 

In this case, you should run proc contents on your data to determine exactly what type of variable, character or numeric, that Municipality is in the source data set (one on a SET statement most likely). The Format you created is intended to work with character values and if the variable is numeric that is the error you get. The "fix" would be to create a format named MunicipalityRegion (no $) and remove all the quotes around the numeric values in the Value statement.

 

Second, the reason I say to supply the whole log entry is because you do not show any code using a format named "Regions". So, we need to see the statements that used that format to have a chance.

One suspects that you may have intended to use $Regioncathegory (and why the H) but actually typed "Regions." or "$Regions." somewhere.

 

You can also have odd things occasionally happen when you define a format to be character with the $ in the value, but do not place the target values in quotes. If you expect the Region to actually be numeric you should use 1) INVALUE, 2) name the created informat RegionCathegory (no $) and 3) use Input instead of PUT.

 

Category does not have an H.

 

proc format;
	value $MunicipalityRegion	
	"101","147","155","185","165","151","153","157","159","161",
	"163","167","169","183","173","175","187","201","240","210",
	"250","190","270","260","217","219","223","230","400","411" 
	= "Region Capital"
	"253","259","350","265","269","320","376","316","326","360",
	"370","306","329","330","340","336","390" 
	= "Region Zealand"
	"420","430","440","482","410","480","450","461","479","492",
	"530","561","563","607","510","621","540","550","573","575",
	"630","580" 
	= "Region Southern Denmark"
	"710","766","615","707","727","730","741","740","746","706",
	"751","657","661","756","665","760","779","671","791" 
	= "Region Central Denmark"
	"810","813","860","849","825","846","773","840","787","820",
	"851" 
	= "Region Northern Jutland"
	"   " 
	= "Region Missing";
	invalue RegionCategory
	"Region Capital" = 1
	"Region Zealand" = 2
	"Region Southern Denmark" = 3
	"Region Central Denmark" = 4
	"Region Northern Jutland" = 5
	"Region Missing" = 6;
	
run;

data junk;
   Municipality='101'; /* This way I know that it is a character value*/
   RegionText = put(Municipality , $MunicipalityRegion.);
   Region = put(RegionText , $RegionCathegory.);
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

First thing; when you have any questions about messages, errors or not, you should copy the entire entry for the data step or procedure along with all of the messages. Multiple messages often are related.

 

In this case, you should run proc contents on your data to determine exactly what type of variable, character or numeric, that Municipality is in the source data set (one on a SET statement most likely). The Format you created is intended to work with character values and if the variable is numeric that is the error you get. The "fix" would be to create a format named MunicipalityRegion (no $) and remove all the quotes around the numeric values in the Value statement.

 

Second, the reason I say to supply the whole log entry is because you do not show any code using a format named "Regions". So, we need to see the statements that used that format to have a chance.

One suspects that you may have intended to use $Regioncathegory (and why the H) but actually typed "Regions." or "$Regions." somewhere.

 

You can also have odd things occasionally happen when you define a format to be character with the $ in the value, but do not place the target values in quotes. If you expect the Region to actually be numeric you should use 1) INVALUE, 2) name the created informat RegionCathegory (no $) and 3) use Input instead of PUT.

 

Category does not have an H.

 

proc format;
	value $MunicipalityRegion	
	"101","147","155","185","165","151","153","157","159","161",
	"163","167","169","183","173","175","187","201","240","210",
	"250","190","270","260","217","219","223","230","400","411" 
	= "Region Capital"
	"253","259","350","265","269","320","376","316","326","360",
	"370","306","329","330","340","336","390" 
	= "Region Zealand"
	"420","430","440","482","410","480","450","461","479","492",
	"530","561","563","607","510","621","540","550","573","575",
	"630","580" 
	= "Region Southern Denmark"
	"710","766","615","707","727","730","741","740","746","706",
	"751","657","661","756","665","760","779","671","791" 
	= "Region Central Denmark"
	"810","813","860","849","825","846","773","840","787","820",
	"851" 
	= "Region Northern Jutland"
	"   " 
	= "Region Missing";
	invalue RegionCategory
	"Region Capital" = 1
	"Region Zealand" = 2
	"Region Southern Denmark" = 3
	"Region Central Denmark" = 4
	"Region Northern Jutland" = 5
	"Region Missing" = 6;
	
run;

data junk;
   Municipality='101'; /* This way I know that it is a character value*/
   RegionText = put(Municipality , $MunicipalityRegion.);
   Region = put(RegionText , $RegionCathegory.);
run;
Reeza
Super User
"1" => Character
1 => Numeric

For the warning you need to convert your first format to a numeric format. 


For the error I think you need an informat, not a format. 

 

Modifying your code:

 

proc format;
	value $MunicipalityRegion	
	"101","147","155","185","165","151","153","157","159","161",
	"163","167","169","183","173","175","187","201","240","210",
	"250","190","270","260","217","219","223","230","400","411" 
	= "Region Capital"
	"253","259","350","265","269","320","376","316","326","360",
	"370","306","329","330","340","336","390" 
	= "Region Zealand"
	"420","430","440","482","410","480","450","461","479","492",
	"530","561","563","607","510","621","540","550","573","575",
	"630","580" 
	= "Region Southern Denmark"
	"710","766","615","707","727","730","741","740","746","706",
	"751","657","661","756","665","760","779","671","791" 
	= "Region Central Denmark"
	"810","813","860","849","825","846","773","840","787","820",
	"851" 
	= "Region Northern Jutland"
	"   " 
	= "Region Missing";
	invalue $ RegionCathegory
	"Region Capital" = 1
	"Region Zealand" = 2
	"Region Southern Denmark" = 3
	"Region Central Denmark" = 4
	"Region Northern Jutland" = 5
	"Region Missing" = 6;
	
run;

Then you can use them as follows:

 

RegionText = put(Municipality , MunicipalityRegion.);
Region = input(RegionText , $RegionCathegory.);

FYI not sure if cathegory should be spelled as category or with the h....

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