BookmarkSubscribeRSS Feed
Aar684
Calcite | Level 5
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.
13 REPLIES 13
Aar684
Calcite | Level 5
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.
Cynthia_sas
SAS Super FREQ
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
Aar684
Calcite | Level 5
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
Peter_C
Rhodochrosite | Level 12
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
Aar684
Calcite | Level 5
Peter. While that does keep the .00 on the whole numbers, it does not keep the decimal point in line.
Cynthia_sas
SAS Super FREQ
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
Aar684
Calcite | Level 5
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 ?
Cynthia_sas
SAS Super FREQ
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]
ChrisNZ
Tourmaline | Level 20
You can use the @ sign, as in:

data _null_;
set sashelp.air ;
put date @20 air;
run;
Aar684
Calcite | Level 5
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?
Tim_SAS
Barite | Level 11
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]
kannareddy0
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 6321 views
  • 0 likes
  • 6 in conversation