Reading a value that does not contain a constant format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Reading a value that does not contain a constant format

I have a file I'm trying to read in but have discovered something odd.  The file was created with zoned decimal format for negative numbers (format ZD7.2) and regular non-signed numeric for positive numbers (7.2).

Is there a good way to read in the file while reading the correct values?  Or do I just need to read it in as text and evaluate each one before setting a value?

Example:

001234N    (-123.45)

0012345    (123.45)


Accepted Solutions
Solution
‎07-23-2013 11:39 PM
Super User
Super User
Posts: 6,502

Re: Reading a value that does not contain a constant format

At which point you need to introduce some program logic into the solution.

67   data test ;

68      input string $ 1-7 ;

69      if index(string,'.') then fmt='BEST7.';

70      else fmt='ZD7.2';

71      number = inputn(string,fmt);

72      put number= Z8.2 string= fmt=;

73   cards;

number=-0123.45 string=001234N fmt=ZD7.2

number=00123.45 string=0012345 fmt=ZD7.2

number=00123.45 string=123.450 fmt=BEST7.

View solution in original post


All Replies
Frequent Contributor
Posts: 129

Re: Reading a value that does not contain a constant format

Have you tried reading all records with ZD7.2 format?

On windows with SAS 9.2, this seems to work on my system for the values you send.

  data test ;

     input number zd7.2 ;

     put number= 8.2 ;

    datalines ;

001234N   

0012345

;;;;

292    data test ;
293       input number zd7.2 ;
294       put number= 8.2 ;
295      datalines ;

number=-123.45
number=123.45

Occasional Contributor
Posts: 7

Re: Reading a value that does not contain a constant format

Thanks for the prompt reply!

Yes, I just tried that and it works.  I had not thought it would read both formats successfully.

Now let me throw another one out there. Same scenario except it's numbers such as 001234N for negatives and then the value 123.45 for positives (where a decimal is included on the file).

Solution
‎07-23-2013 11:39 PM
Super User
Super User
Posts: 6,502

Re: Reading a value that does not contain a constant format

At which point you need to introduce some program logic into the solution.

67   data test ;

68      input string $ 1-7 ;

69      if index(string,'.') then fmt='BEST7.';

70      else fmt='ZD7.2';

71      number = inputn(string,fmt);

72      put number= Z8.2 string= fmt=;

73   cards;

number=-0123.45 string=001234N fmt=ZD7.2

number=00123.45 string=0012345 fmt=ZD7.2

number=00123.45 string=123.450 fmt=BEST7.

Occasional Contributor
Posts: 7

Re: Reading a value that does not contain a constant format

Thanks Tom, this helped a lot!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 341 views
  • 3 likes
  • 3 in conversation