SAS Programming

DATA Step, Macro, Functions and more
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
SAS Super FREQ

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 2955 views
  • 1 like
  • 4 in conversation