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
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.
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.
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.
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.