Data step

Occasional Contributor
Posts: 6

Data step

Hi,

I have my dataset for the banking data as mentioned below.

data test;
input varname\$;
cards;
12+
-10
50+
-1
-45
300+
;
run;

I am expecting the output as mentioned below. It means numeric sign should come after the number also the variable should be in numeric not character. Anyone can help me ?

12+
10-
50+
1-
45-
300+

Thanks,
Regular Contributor
Posts: 241

Re: Data step

Here is one way, which has a couple of severe limitations: (1) It cannot handle decimals; and (2) it truncates if there are more digits than specified in the format (here it is five).

/* test data */

data one;

input var \$ @@;

cards;

12+ -10 50+ -1 -45 300+ 0 .a . 234567 0.123+

;

run;

proc format;

picture trailsgn(default=6

low-<0='00000-'

0<-high='00000+';

run;

data two;

set one;

num = input(var, trailsgn.);

format num trailsgn.;

run;

/* check */

proc print data=two;

run;

/* on list

Obs    var        num

1    12+          12+

2    -10          10-

3    50+          50+

4    -1            1-

5    -45          45-

6    300+        300+

7    0              0

8    .a             A

9                   .

10    234567    34567+

11    0.123+

*/

PROC Star
Posts: 8,163

Re: Data step

Chang,

The trailsgn informat can handle both decimals and commas and can be up to 32 characters long. I would just read the data in that way and assign a long enough picture format. E.g.,:

proc format;
picture sgn
low-<0 = "0000000000.00-"
0-high = "0000000000.00+";
run;

data test;
input varname trailsgn20.;
format varname sgn.;
cards;
12+
10-
-114.67
25,899,999.62
50+
1-
45-
300+
;
run;

Art
PROC Star
Posts: 8,163

Re: Data step

For some reason my code didn't appear completely:

The trailsgn informat can handle both decimals and commas and can be up to 32 characters long. I would just read the data in that way and assign a long enough picture format. E.g.,:

proc format;
picture sgn
low-<0 "0000000000.00-"
0-high "0000000000.00+";
run;

data test;
input varname trailsgn20.;
format varname sgn.;
cards;
12+
10-
-114.67
25,899,999.62
50+
1-
45-
300+
;
run;

Note change the above to an actual equal sign. Can someone tell me how to enter such a sign in these posts? When I just type them, they disappear from the message.
Super Contributor
Posts: 394

SAS Super FREQ
Posts: 9,365

Re: Data step

Hi:
"low -<10000" can be posted to the forum...so can:
[pre]
if age >= 15 then....
where amount < 64 ...
etc, etc
[/pre]
because you "protect" the < sign by using &lt; in your posted code. When I am posting code to the forum with < or > signs, I generally search and replace the < signs in the SAS code with &lt; and the > signs with &gt;. (Then I cut and paste the code to the forum ...and quit my SAS or Notepad editor so the code changes are not saved.)

The reason your code appears to be truncated (and why your matching <equal sign> worked) is that the forum posting mechanism uses HTML behind the scenes and <tagname> is used in HTML -- so a beginning < starts the forum posting mechanism to look for a valid tagname and it keeps grabbing characters after the < until it finds the closing > for the tagname. With unmatched symbols (as with an opening <, but not a closing >), it never finds the end of the beginning bracket.) When the forum posting software found <equal sign> it had a matching set of < and > with an invalid HTML tagname between the 2 brackets, so the string was allowed to go into the post untruncated.

This previous forum posting explains how to protect < and > in more detail and has other useful information:

cynthia
Regular Contributor
Posts: 241

Re: Data step

> proc format;

> picture sgn

> low-<0 "0000000000.00-"

> 0-high "0000000000.00+";

> run;

...

I have been burnt by something similar to your format above. Mixing picture formats and decimal numbers can be an excellent way to shoot yourself in the foot.

/* tail sign format by art297 */

proc format;

picture sgn

low-<0 = "0000000000.00-"

0-high = "0000000000.00+";

run;

/* ouch! */

data _null_;

v = -0.12;

put v= :sgn8.;

run;

/* on log

v=12-

*/

Frequent Contributor
Posts: 81

Re: Data step

How about slightly changing the format?

proc format;
picture sgn
low-<0 "0000000009.00-"
0-high "0000000009.00+";
run;

Message was edited by: Nick R Message was edited by: Nick R
Super User
Posts: 10,761

Re: Data step

[html]
Nick is right.

proc format;
picture sgn
low-<0 = "0000000009.99-"
0-high = "0000000009.99+";
run;

[/html]
Occasional Contributor
Posts: 6

Re: Data step

Thank you all,

My issue solved with your valuable suggestions.