BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
noda6003
Quartz | Level 8

i have values as

val

100

98

37.09876554

32.7653243

76

 

and i want to have length as 5.2 only if i have integer and i use val1=input(val,best4.) but somehow it gives blank whenevr i have whole digits such as 100, 98, 76 and makes the deimals 37.09.

 

Is there anyway where i can keep whole digits as it is reduce decimals?

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

You can use the bestd format like this

 

data have;
input val;
format val bestd6.2;
datalines;
100
98
37.09876554
32.7653243
76
;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

I'm not sure I understand this. You want to display integers without decimals and non-integers with two decimals, or?

noda6003
Quartz | Level 8

yes, integers without decimals and non-integers with 2 decimals

PeterClemmensen
Tourmaline | Level 20

You can use the bestd format like this

 

data have;
input val;
format val bestd6.2;
datalines;
100
98
37.09876554
32.7653243
76
;
Cynthia_sas
Diamond | Level 26

Hi:

  Internally, as SAS stores the data, whether or not there are decimals doesn't make a difference. The only place it makes a difference is in the display of the values, such as in a report. PROC PRINT won't allow a different format for every variable value -- unless you convert your number to a character string. However, PROC REPORT will allow you to use a different format for different values in a column.

 

  For example, consider the program and output below:

diff_fmt_proc_report.png

 

The "helper" variable TESTDEC is being used as a simple way to tell whether the number needs decimal places in the format or not.

use_testdec.png

CALL DEFINE with PROC REPORT allows you to apply a format conditionally to a data cell. CALL DEFINE can only be used in a COMPUTE block, as shown.

 

Hope this helps point you in a direction,

 

Cynthia

 

 

Krueger
Pyrite | Level 9
data have;
	input Var;
	datalines;
	100
	98
	37.0834224
	32.766523
	76
	;
run;

data want;
	set have;
	if find(Var, '.', '') ge 1 then Test = round(var,.1);
	else Test = Var;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3182 views
  • 1 like
  • 4 in conversation