BookmarkSubscribeRSS Feed
Sarojkumar
Calcite | Level 5
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.
9 REPLIES 9
ArtC
Rhodochrosite | Level 12
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]
Peter_C
Rhodochrosite | Level 12
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?
ArtC
Rhodochrosite | Level 12
Thanks Peter - and I learned something that is not just about semi-colons 🙂
deleted_user
Not applicable
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
ArtC
Rhodochrosite | Level 12
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.
deleted_user
Not applicable
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
data_null__
Jade | Level 19
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.
Peter_C
Rhodochrosite | Level 12
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
Ksharp
Super User
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

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