BookmarkSubscribeRSS Feed
Hansmuffs
Fluorite | Level 6

Hey,

 

I want to change a datetyp of column and I'm not sure if this is the correct way to do this. I mean my solution works, but is their a possibility to define the new datatyp directly in the attrib function? Maybe with the informat definition. I'm not really sure what informat is and when I should use this.

 

data inputData;
	input	
		name 	$
		amount	$;
	datalines;
		Peter 100
		Kai 25
	;
run;

data outputData;
	attrib	
		name 	format = $10.
		amount  format = $10.
		/* amount informat = $10. format 10.*/
	;
	set inputData;
	amount_tmp = input(amount, 10.);
	drop amount;
	rename amount_tmp = amount;
run;

 

Thanks 

Hans

4 REPLIES 4
Astounding
PROC Star
You do need to jump through the drop/rename hoop. However, the program works without the attrib statement. That can be removed.

Also note, the top DATA step assigns AMOUNT a length of $8. If that's the case (and I realize your post might be a simplification of your actual program), your solution still works. But you need to read only 8 characters not 10 in the formula for AMOUNT_TMP.

Hansmuffs
Fluorite | Level 6

I need to change datatype from character to numeric. 

When I change dataype from format 10. to 8. maybe when I have long numbers 12345678910 some numbers at the end will be cut?!

 

data outputData_V3;
	set inputData;
	amount = input(amount, 8.);
	amount_final = input(amount, 8.);
	drop amount;
	rename amount_final = amount;
run;

I don't know how to change the datatyp without drop and rename because as you can see amount = input(amount, 8.) will not create a numeric value it is still character. But the amount_final is in numeric format.

Astounding
PROC Star

OK, let's go back to your original program, with ATTRIB cut out:

data inputData;
	input	
		name 	$
		amount	$;
	datalines;
Peter 100
Kai 25
;

data outputData;
	set inputData;
	amount_tmp = input(amount, 10.);
	drop amount;
	rename amount_tmp = amount;
run;

This should work.  Yes, this never works:  amount = input(amount, 10.);

 

The top DATA step gives AMOUNT a length of $8.  It could never store "1234567890".  In real life, that may not be how AMOUNT got created, so it may actually have a longer length.  Within the INPUT function, use whatever length is actually assigned to AMOUNT.

andreas_lds
Jade | Level 19

Why is it necessary to change the variable type? I would fix the importing process. If that can't be done, using the input-function is the way to go.

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 724 views
  • 0 likes
  • 3 in conversation