Reputation: 389
I am new to programming in SAS. I have a table called cohort_sc with the column MRN stored as a character with leading zeros. To remove the leading zeros I cast it into an int and back to a character. But, when I run any other code after this, the two new columns mrn_int and mrn_char are no longer stored in cohort_sc. How can I save the new columns?
data cohort_sc;
set cohort_sc;
MRN_INT = INPUT(MRN,best.);
MRN_char = PUT(MRN_INT,12.);
set cohort_sc;
run;
Upvotes: 0
Views: 162
Reputation: 51621
Don't overwrite your existing dataset. that will make it hard to debug when you make mistakes as you will have to recreate the original dataset to try again.
Your code should work, but you might want to make sure that the new variables are defined as you want instead of forcing SAS to GUESS how you want to define them.
Note that BEST
is the name of a FORMAT, not an INFORMAT. The INPUT() function does not care if the width of the informat is more than the length of the string you are reading, so just use 32.
as then informat as that is the maximum number of characters it can read.
The 12.
format will right align the digit strings. Do you really want leading spaces in your new MRN_CHAR variable?
data new_cohort_sc;
set cohort_sc;
length MRN_INT 8 MRN_char $12 ;
MRN_INT = INPUT(MRN,32.);
MRN_char = left(PUT(MRN_INT,12.));
run;
You could do it without making the numeric value by using the VERIFY() function to find the location of the first character that is not a '0' in your existing MRN variable.
data new_cohort_sc;
set cohort_sc;
length MRN_char $12 ;
MRN_char = substr(MRN,max(1,verify(MRN,'0')));
run;
Upvotes: 1