Contributor
Posts: 54

# Decimal Point

Is there anyway to keep a decimal point in the same position in SAS dataset?

Normally SAS places numeric values right justified. Is there any way to keep the decimal point in the same position vertically for every row? Thanks.
Contributor
Posts: 54

## Re: Decimal Point

I am using the code put @20 totalkwh z10.2

This lines up the decimal point but does not show zeroes after the decimal point like I need. Some values are whole numbers and have no fraction but I still need to display .00 after the number while keeping all decimal points in line.
SAS Super FREQ
Posts: 9,254

## Re: Decimal Point

Hi:
In your original post, you asked about keeping the decimals lined up in a SAS dataset. Since you don't usually see what's INSIDE a SAS dataset, I wasn't sure why you would care about where the decimal point is in the dataset's internal storage.

However, since you show using a PUT, it seems that you might want a -REPORT-??? with the data showing as decimal aligned,

What would be useful would be to see more of your code. For example are you writing to a flat file??
[pre]
filename flatfile 'c:\temp\flat.txt';
...and then in your DATA step:
file flatfile;
put @10 totalkwh z10.2;
???

OR, are you doing:
file print;
put @10 totalkwh z10.2;
???

OR, are you doing:
file print ods;
put @10 totalkwh z10.2;
???
[/pre]

If you are using FILE PRINT ODS -- then what is your destination of interest???

cynthia
Contributor
Posts: 54

## Re: Decimal Point

Cynthia. Sorry for my lack of knowledge.

What I am trying to do exactly is take a SAS dataset that has 2 columns. One column is 9 digit account numbers with the other being electricity usage values with some having a decimal values (ie 8654.56) while others are whole numbers (655322).

I need to ouput a flat file that left justifies the first column and keeps the decimal points aligned in the second column, while adding .00 to the whole number values. Any advice? My knowledge isn't what it needs to be I know. Message was edited by: Aar684
Valued Guide
Posts: 2,190

## Re: Decimal Point

put account_number @ 10 usage 12.2 ;

that "12.2" is a format, which is good for values up to 999999999.99

I guessed the names of the columns in your input.
Since I don't know the name of your input, nor in what file you want the data written, these parts of the data step are not there - just the put statement that guarantees the position length and format of the usage value. The account number will be left aligned starting in the first column

hope it helps
Contributor
Posts: 54

## Re: Decimal Point

Peter. While that does keep the .00 on the whole numbers, it does not keep the decimal point in line.
SAS Super FREQ
Posts: 9,254

## Re: Decimal Point

Hi:
For comparison purposes, here's some data and code to output TYPE and VALUE to a flat file. Note that VALUE is written multiple times using multiple formats. The -l modifier on the PUT will ensure that something is left justified as a result of the PUT statement.

For example, if I have this data and PUT code:
[pre]
DATA new;
length type \$9;
INPUT type \$ value;
DATALINES;
q12345678 .5
r12345678 .025
s12345678 1.75
t12345678 0.75
u12345678 10
v12345678 1.9999
w12345678 .75
x12345678 33.333
y12345678 44.4111
z12345678 555
;
RUN;

ods listing;
data _null_;
file 'c:\temp\flatfile.txt';
set new;
if _n_ = 1 then
put @10 'type'
@21 'use z10.2'
@37 'use d10.0'
@48 'Use 10.2'
@60 'Use 10.2 -l'
@74 'Use BEST -l';
put @10 type
@20 value z10.2
@36 value d10.0
@46 value 10.2
@60 value 10.2 -l
@74 value best12.2 -l;
run;
[/pre]

I get this output in the FLATFILE.TXT file:
[pre]
type use z10.2 use d10.0 Use 10.2 Use 10.2 -l Use BEST -l
q12345678 0000000.50 0.50000 0.50 0.50 0.5
r12345678 0000000.03 0.02500 0.03 0.03 0.025
s12345678 0000001.75 1.75000 1.75 1.75 1.75
t12345678 0000000.75 0.75000 0.75 0.75 0.75
u12345678 0000010.00 10.00000 10.00 10.00 10
v12345678 0000002.00 1.99990 2.00 2.00 1.9999
w12345678 0000000.75 0.75000 0.75 0.75 0.75
x12345678 0000033.33 33.33300 33.33 33.33 33.333
y12345678 0000044.41 44.41110 44.41 44.41 44.4111
z12345678 0000555.00 555.00000 555.00 555.00 555
[/pre]

Note how 10 and 555 both have .00 added to them with all formats except the BEST format.

cynthia
Contributor
Posts: 54

## Re: Decimal Point

Thank you so much everyone. I have what I need in this regard. I do however have another question.

I am writing out to a flat file my two columns of data. The software the flat file is being imported into requires 8 blank spaces after the 2nd column. I've tried adding a column with a blank variable which works in SAS but does not keep the extra spaces upon exporting. Any ideas ?
SAS Super FREQ
Posts: 9,254

## Re: Decimal Point

Hi:
I'm not sure I understand what you mean when you say that:
I've tried adding a column with a blank variable which works in SAS but does not keep the extra spaces upon exporting.

Once you've created the flat file with SAS, SAS is out of the picture -- if the blanks are there when SAS creates the file, anything else -- such as the treatement of those blanks or spaces is the responsibility of the importing program. Are you FTP'ing the file to another platform? Is there a setting that is stripping "white space" out of the file??

When I run the program below, I can verifiy (with Notepad) that the 8 blank characters are actually in the file, as put there by SAS. Whether the spaces are respected on IMPORT into another software application sort of depends on how that software application treats blanks at the end of a line in an ASCII text file.

Can you elaborate more??

cynthia
[pre]
DATA new;
length type \$9;
INPUT type \$ value;
DATALINES;
q12345678 .5
r12345678 .025
s12345678 1.75
t12345678 0.75
u12345678 10
v12345678 1.9999
w12345678 .75
x12345678 33.333
y12345678 44.4111
z12345678 555
;
RUN;

ods listing;
data _null_;
file 'c:\temp\flatfile2.txt';
set new;
if _n_ = 1 then
put @10 'type'
@20 'use z10.2'
@30 ' ';
put @10 type
@20 value z10.2
@30 ' ';
run;
[/pre]
PROC Star
Posts: 2,220

## Re: Decimal Point

You can use the @ sign, as in:

data _null_;
set sashelp.air ;
put date @20 air;
run;
Super Contributor
Posts: 394

Contributor
Posts: 54

## Re: Decimal Point

Tim, yes. Thank you for the quick response.

I've gotten the decimal point to stay in-line. I am having trouble getting my whole numbers to show .00

90% of the values have fractions (ie .75) but some don't. In order for the decimals to stay in-line .00 needs to display on the whole numbers without fractions. Is there anyway to do this?
Super Contributor
Posts: 394

## Re: Decimal Point

As long as the values are "similar magnitude" the Dw.p format adds the .00 to inegers. Below, the d6.2 format formats 1 as 1.00.
[pre]
data;
input x;
datalines;
1.2
1.25
1.257
1
1.5
1.75
;;;;
proc print;
format x d6.2;
run;
[/pre]

[pre]
Obs x

1 1.20
2 1.25
3 1.26
4 1.00
5 1.50
6 1.75
[/pre]
Senior User
Posts: 1

## Re: Decimal Point

if the magnitude is different, is there any format to add the decimals to integers?

Discussion stats
• 13 replies
• 3114 views
• 0 likes
• 6 in conversation