DATA Step, Macro, Functions and more

Numeric variable with +/-ve sign and leading zeros.

Reply
Occasional Contributor
Posts: 14

Numeric variable with +/-ve sign and leading zeros.

Is there any format to write the numeric variables with the +ve or -ve sign and with leading zeros e.g


if its 3.5, it should be +0003.5
if its 23.6, it should be +0023.6
if its -23.6 it should be -0023.6

The Zw.d format is used to add leading zeros and it works for negative numeric values but how to assign the +ve sign before the positive numeric data.
Valued Guide
Posts: 634

Re: Numeric variable with +/-ve sign and leading zeros.

Posted in reply to Sarojkumar
You can build your own format with the PICTURE statement in PROC FORMAT. Adapt the following, which should be close.

For some reason the code is being truncated and does not show up in the forum. email me and I will send it to you.
art@caloxy.com

[pre]
proc format;
picture ve
low - < 0 = '0999.9'(prefix='-')
0 = '9'
0 < - high = '0999.9' (prefix='+');
run;

data vals;
x=-23.4; output;
x=1.5; output;
x=0; output;
x=0.78; output;
x=91.34; output;
run;
proc print data=vals;
format x ve.;
run;
[/pre]
Valued Guide
Posts: 2,177

Re: Numeric variable with +/-ve sign and leading zeros.

for explanation of how to use the forum "markup" see
http://support.sas.com/forums/thread.jspa?messageID=27609毙
I really think this might make a useful link on the "Post Message: Reply" toolbar

In the situation where the message you are interested in has partly disappeared, you can reveal most of the message (here from ArtC) with the "Quote Original" button in the "Post Message: Reply" frame

> You can build your own format with the PICTURE
> statement in PROC FORMAT. Adapt the following, which
> should be close.
>
> For some reason the code is being truncated and does
> not show up in the forum. email me and I will send
> it to you.
> art@caloxy.com
>
> [pre]
> proc format;
> picture ve
> low - < 0 = '0999.9'(prefix='-')
> 0 = '9'
> igh = '0999.9' (prefix='+');
> run;
>
> data vals;
> x=-23.4; output;
> x=1.5; output;
> x=0; output;
> x=0.78; output;
> x=91.34; output;
> run;
> proc print data=vals;
> format x ve.;
> run;
> [/pre]

Most of the problem has been caused by the less-than symbol used in trying to exclude 0 from the negative range
(I don't know if that could become default behaviour, but I would certainly vote for it in a sasware ballot!!! see http://support.sas.com/community/ballot/)
Here is my markup with the "special" you can reveal with "Quote Original" (, I hope)
[pre] proc format cntlout=cntlout ;
picture ve
low - 0 = '0999.9' (prefix='-')
0 - 0 =' 0000.0' ( noedit )
0 - high = '0999.9' (prefix='+');
run;

%put %sysfunc( putn( 123, ve ))
%sysfunc( putn( 1.2, ve ))
%sysfunc( putn(-1.2, ve ))
%sysfunc( putn( 0, ve ));[/pre]This puts this line into the log [pre]+123.0 +001.2 -001.2 -000.0[/pre]demonstrating that the 0 is included in the negative range, even though it has its own range ?!?!?
So here is proc format code with "zero exclusion" on the negative range (and a demo of the markup which allows a "less than" symbol [pre]
proc format cntlout=cntlout ;
picture ve
0 - high = '0999.9' (prefix='+')
low - <0 = '0999.9' (prefix='-') ;
run;

%put %sysfunc( putn( 123, ve ))
%sysfunc( putn( 1.2, ve ))
%sysfunc( putn(-1.2, ve ))
%sysfunc( putn( 0, ve ));[/pre] clipped from the saslog, proving zero is no longer in the negative range[pre]+123.0 +001.2 -001.2 +000.0[/pre]
Does the SASware ballot extend to recommendations for change to this Forum?
Valued Guide
Posts: 634

Re: Numeric variable with +/-ve sign and leading zeros.

Thanks Peter - and I learned something that is not just about semi-colons :-)
N/A
Posts: 0

Re: Numeric variable with +/-ve sign and leading zeros.

How about reading numeric variable with +/1 sign ?
e.g.
+1234567.89
-1234567.89

I tried informat PD10.2, ZD10.2 didn't work.

PLease help.
Mae
Valued Guide
Posts: 634

Re: Numeric variable with +/-ve sign and leading zeros.

Posted in reply to deleted_user
You should not need an INFORMAT to read positive or negative numbers. The positive numbers are correctly read in with or without the plus sign.
[pre]data posneg;
input value;
datalines;
+123.12
-123.12
345.2222
-.003
run;
proc print data=posneg;
run;
[/pre]
If you need to display the plus sign for positive numbers you can use the appropriate PICTURE format shown above.
N/A
Posts: 0

Re: Numeric variable with +/-ve sign and leading zeros.

ArtC,

Thank you. Yes, use datalines doesn't need informat.
My question is an IBM mainframe question
I am reading a COBOL file containing dollar amount field with +/- sign. The field is a s9(10)v9(2) signed packed 10 digit with 2 decimal. Just want to know how to define it in input statement:
This is my code
Data test;
INFILE IN ;
INPUT @01 CO $CHAR5.
@40 BEG_BAL PD10.2 <-??
@51 END_BAL PD10.2 <-??
PROC PRINT DATA=TEST;

The result is blank for beg_bal and end_bal.

Mae
Respected Advisor
Posts: 3,799

Re: Numeric variable with +/-ve sign and leading zeros.

Posted in reply to deleted_user
Usage Note 3714: SAS informats that correspond to COBOL data descriptions

http://support.sas.com/kb/3/714.html

Search at support.sas.com for more info related to COBOL ins and outs.
Valued Guide
Posts: 2,177

Re: Numeric variable with +/-ve sign and leading zeros.

Posted in reply to deleted_user
10 +2 +sign (half bytes)
= PD7.2 (where that 7 is a counter of sufficient bytes to hold 10+2+1
half-bytes
> Data test;
> INFILE IN ;
> INPUT @01 CO $CHAR5.
> @40 BEG_BAL PD10.2 <-??

try
@40 BEG_BAL PD7.2
Super User
Posts: 10,044

Re: Numeric variable with +/-ve sign and leading zeros.

Posted in reply to Sarojkumar
Thanks. Peter. you get up early.

[pre]
proc format;
picture pct
low -< 0 = '09999.9' (prefix='-')
0 - high = '09999.9' (prefix='+');
run;
data temp;
input value;
format value pct.;
[/pre]

Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 9 replies
  • 5637 views
  • 0 likes
  • 6 in conversation