Read packed decimal values and write it

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Read packed decimal values and write it

Hi,

I am trying to read some non-readable values from a dataset and write it as numeric field. But I am not getting the exact output.

Field legth in inputfile is 5 bytes. and non-readable format.In output, I need to write the same as follows:

Output:

Field length should be 13 bytes with two decimal points and with leading spaces:

'          01.50'

'          13.62'

'        100.21'

'          81.1'

Please help me. I am new to SAS and handling packed decimal values for first time.

Thank you.


Accepted Solutions
Solution
‎08-19-2015 02:52 PM
Super User
Super User
Posts: 6,843

Re: Read packed decimal values and write it

Why would you NOT want the zero before the decimal place?

SAS does not really support that format directly, but you could perhaps build your own.

proc format ;

  picture nozero

  low- -1='000000000.99' (prefix='-')

  -1-<0 = '99' (prefix='         -.' multiplier=100)

  0-1 = '99' (prefix='          .' multiplier=100)

  1-high='0000000000.99'

;

run;

data _null_;

  input x @@;

  put x 13.2 +1  x nozero.;

cards;

-1 1.3 0.34 0.25 -.24 1.24 0

;;;;

        -1.00         -1.00

         1.30          1.30

         0.34           .34

         0.25           .25

        -0.24          -.24

         1.24          1.24

         0.00           .00

View solution in original post


All Replies
Super User
Posts: 19,064

Re: Read packed decimal values and write it

What are you trying to read from (CSV, TXT, DB) and how did you read it, what was your code?

Contributor
Posts: 20

Re: Read packed decimal values and write it

I am trying to read from a flat file in mainframes. Fields are in fixed positions.

Input File:

Data ifile;

@01 Name $8.

@09 Age 02.

@11 Appthrs pd 5.;

datalines;

Johhny 25 $@%%^

David  54 *&$#@

Output:

date ofile:

set ifile;

format appthrs pd5;

Put

@01 Name $8.

@09 Age 02.

@11 Appthrs 13.2.;

Expected output:

Johhny 25 12.21

David  54 05.50

Super User
Posts: 11,115

Re: Read packed decimal values and write it

Your last value does not display two decimals.

Also what is your target destination. I would assume text but ...

And should the output be actually quoted or was that just to show the leading spaces?

If writing to text then an F13.2 format should work unless you actually need the quotes.

If the input looked like 10021 and needs to be read as 100.21 then an input format of F5.2 would work. Put to assign an input format you'll need to use a data step.

Super User
Super User
Posts: 6,843

Re: Read packed decimal values and write it

If you really have PD data with two decimal points then you should use a program like this:

data have ;

  infile 'source data file' ;

  file 'new source data file' ;

  input name $8. age 2. appthrs S370FPD5.2;

  put name $8. age 2. appthrs 13.2 ;

run;


If you data really has values like you presented then the PD format for those values would look like these HEX codes.

x=1.5    y=000000150C

x=13.62  y=000001362C

x=100.21 y=000010021C

x=81.1   y=000008110C

Contributor
Posts: 20

Re: Read packed decimal values and write it

Hi Tom,

Thank you.

Its working fine. But, if value is .25 in input dataset, then I am getting output with leading zero i.e 0.25. How to remove the zero from that field in output?

Solution
‎08-19-2015 02:52 PM
Super User
Super User
Posts: 6,843

Re: Read packed decimal values and write it

Why would you NOT want the zero before the decimal place?

SAS does not really support that format directly, but you could perhaps build your own.

proc format ;

  picture nozero

  low- -1='000000000.99' (prefix='-')

  -1-<0 = '99' (prefix='         -.' multiplier=100)

  0-1 = '99' (prefix='          .' multiplier=100)

  1-high='0000000000.99'

;

run;

data _null_;

  input x @@;

  put x 13.2 +1  x nozero.;

cards;

-1 1.3 0.34 0.25 -.24 1.24 0

;;;;

        -1.00         -1.00

         1.30          1.30

         0.34           .34

         0.25           .25

        -0.24          -.24

         1.24          1.24

         0.00           .00

Contributor
Posts: 20

Re: Read packed decimal values and write it

Thank you so much.. It worked well.

Super User
Posts: 3,233

Re: Read packed decimal values and write it

The links in this post could be useful:

https://communities.sas.com/message/30631#30631

Super User
Posts: 11,115

Re: Read packed decimal values and write it

Also is this IMPLIED decimals, which is just showing the number without a decimal point and is assumed to be in a certain location or actual packed decimal which is a specific form of encoding data in binary values.

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 697 views
  • 2 likes
  • 5 in conversation