I have a table with several observations with 16-digit numeric codes that I need to convert to text because when I send my output from EG to an excel, I don't want the last digit of my numbers being converted to 0 (since excel can only handle 15 numeric numbers but no constraints on texts). However, when I convert these 16-digit numeric codes to text, the new column cuts down my numbers.
Dataset "ABC":
obs code
1 1234567890123456
2 2345678901234567
Code:
data temp;
set ABC;
New_code= put(code, $16.);
format New_code $16.;
run;
Results:
obs Code New_code
1 1234567890123456 123456789012
2 2345678901234567 234567890123
The results I want:
obs Code New_code
1 1234567890123456 1234567890123456
2 2345678901234567 2345678901234567
If variable CODE is really numeric, then the program you present should generate a warning message.
Did you actually try this program code? What do you see in the sas log?
But even in spite of this warning message, I cannot reproduce your results. I see what you initially expected. See below:
Data ABC;
input obs code;
datalines;
1 1234567890123456
2 2345678901234567
run;
data temp;
set ABC;
New_code= put(code, $16.);
format New_code $16.;
put (_all_) (=);
run;
which produces 16 character values for new_code, per the log below:
32 data temp;
33 set ABC;
34 New_code= put(code, $16.);
WARNING: Variable code has already been defined as numeric.
35 format New_code $16.;
36 put (_all_) (=);
37 run;
obs=1 code=1.2345679E15 New_code=1234567890123456
obs=2 code=2.3456789E15 New_code=2345678901234567
And of course to eliminate the warning, the new_code= assignment statement should be
New_code= put(code, 16.);
If variable CODE is really numeric, then the program you present should generate a warning message.
Did you actually try this program code? What do you see in the sas log?
But even in spite of this warning message, I cannot reproduce your results. I see what you initially expected. See below:
Data ABC;
input obs code;
datalines;
1 1234567890123456
2 2345678901234567
run;
data temp;
set ABC;
New_code= put(code, $16.);
format New_code $16.;
put (_all_) (=);
run;
which produces 16 character values for new_code, per the log below:
32 data temp;
33 set ABC;
34 New_code= put(code, $16.);
WARNING: Variable code has already been defined as numeric.
35 format New_code $16.;
36 put (_all_) (=);
37 run;
obs=1 code=1.2345679E15 New_code=1234567890123456
obs=2 code=2.3456789E15 New_code=2345678901234567
And of course to eliminate the warning, the new_code= assignment statement should be
New_code= put(code, 16.);
Why is code created as numeric in the first place? This points to a faulty data import process, most probably caused by the use of PROC IMPORT.
Yes, I initially converted an excel file to a CSV file, then used proc import. The field in the excel file was in a text format, but when I imported it, it became a numeric field in the sas data set. Do you know how to keep that field a text field when converting it to a sas data set?
@ChickenLittle wrote:
Yes, I initially converted an excel file to a CSV file, then used proc import. The field in the excel file was in a text format, but when I imported it, it became a numeric field in the sas data set. Do you know how to keep that field a text field when converting it to a sas data set?
Write the data step to read the file yourself. Then you have complete control over how the variable is defined.
So if you have this CSV file:
obs,code 1,1234567890123456 2,2345678901234567
The code is just:
data want;
infile CSV dsd firstobs=2 truncover;
input obs code :$16.;
run;
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.