BookmarkSubscribeRSS Feed
MJyothi
Calcite | Level 5
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,
9 REPLIES 9
chang_y_chung_hotmail_com
Obsidian | Level 7
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+


   */

art297
Opal | Level 21
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
art297
Opal | Level 21
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.
Cynthia_sas
SAS Super FREQ
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:
http://support.sas.com/forums/thread.jspa?messageID=27609毙

cynthia
chang_y_chung_hotmail_com
Obsidian | Level 7
> 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-


   */

NickR
Quartz | Level 8
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
Ksharp
Super User
[html]
Nick is right.

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

[/html]
MJyothi
Calcite | Level 5
Thank you all,

My issue solved with your valuable suggestions.


Thanks for your time!

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
  • 1182 views
  • 0 likes
  • 7 in conversation