DATA Step, Macro, Functions and more

Display variable properly (BESTw.d)

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Display variable properly (BESTw.d)

I'm on SAS version 5.1.

 

In a database I have a numeric variable called CtrcN whose entries are 14 digit numbers. They are displayed as, for example, 4.2000001E13 instead of 42000001251100.

In order to try to fix this I use the following code.

DATA nameofoutputdatabase;
SET nameofinputdatabse;
CtrcN_Aux=input(CtrcN,BEST14.);
RUN;

(I used an auxiliary variable just to avoid possible issues with writing over the same variable).

The variable CtrcN_Aux still comes looking like 4.2000001E13.

A possible fix to this is to change it into a character variable, but I don't want this. Another one is to define the format in a PROC SQL, which I also do not want to do.

What can I do in order to display the variable as 42000001251100 instead of 4.2000001E13 by using a DATA ; SET ; RUN ;?

Thanks.


Accepted Solutions
Solution
‎04-13-2016 07:15 AM
Super Contributor
Posts: 308

Re: Display variable properly (BESTw.d)

Posted in reply to sleretrano

hello,

 

try applying the format to the variable: format CtrcN best14.;

View solution in original post


All Replies
Solution
‎04-13-2016 07:15 AM
Super Contributor
Posts: 308

Re: Display variable properly (BESTw.d)

Posted in reply to sleretrano

hello,

 

try applying the format to the variable: format CtrcN best14.;

Contributor
Posts: 46

Re: Display variable properly (BESTw.d)

Perfect. Simple and efficient.
Why isn't my method working, though?
Super Contributor
Posts: 308

Re: Display variable properly (BESTw.d)

Posted in reply to sleretrano

because input is for transforming character variable to numeric format. Your variable is already a numeric variable and if you check

the log you will notice the message:

 Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn)...

 

This means your numeric variable used in the function (CtrcN) is converted by sas to a character one using best12. format.

 

More on input:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000180357.htm

 

Contributor
Posts: 46

Re: Display variable properly (BESTw.d)

Thank you very much.

Very informative.
Trusted Advisor
Posts: 1,118

Re: Display variable properly (BESTw.d)

[ Edited ]
Posted in reply to sleretrano

Hi @sleretrano,

 

-- I just notice that Loko has already given the explanation, while I was writing this --

 

I assume, with "database" you mean "SAS dataset." It was a good idea to store the result of the INPUT function in a new variable, because in general the expression input(CtrcN,BEST14.) returns a different value than CtrcN:

  1. Since the INPUT function expects a character expression as its first argument, the value in CtrcN is automatically converted to a character string (see corresponding NOTE in the log!) using the default numeric format BEST12., which turns 42000001251100 into "4.2000001E13". Please note that the information contained in the last six digits (251100) is already lost at this point!
  2. Reading the string "4.2000001E13" with informat BEST14. results in the number 4.2000001E13, i.e., 42000001000000.

 

This is not what you want. Instead, don't touch the numeric value and just apply an appropriate format like BEST14. for display purposes, as Loko has suggested. If all the numbers in CtrcN are integers of (up to) 14 digits, format 14. would do as well.

 

Contributor
Posts: 46

Re: Display variable properly (BESTw.d)

Posted in reply to FreelanceReinhard
Thank you. I'm new to SAS, I don't know the standard terminology yet. This was helpful.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 457 views
  • 1 like
  • 3 in conversation