BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChickenLittle
Obsidian | Level 7

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 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.);
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

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.);
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ChickenLittle
Obsidian | Level 7

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?

Tom
Super User Tom
Super User

@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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 572 views
  • 3 likes
  • 4 in conversation