DATA Step, Macro, Functions and more

excluding 0 from after a decimal

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

excluding 0 from after a decimal

Hi,

 

How to give required auto format to a NUMERIC variable in a datastep(without Macros)

For eg:

Numaric Variable values are as follow

A = 1.0, 12.30, 11.07, 7.70, 0.0, 17.71

 

I want them to be like

 

Numeric, B = 1, 12.3, 11.07, 7.7, 0,  17.71

 

Thanks in Advance

Santosh

 

 


Accepted Solutions
Solution
‎02-23-2017 10:02 AM
PROC Star
Posts: 308

Re: excluding 0 from after a decimal

Posted in reply to ysantosh18

Doe the "best." format not give you what you want?


data _null_;
do A = 1.0, 12.30, 11.07, 7.70, 0.0, 17.71;
put a 8.2 a best.;
output;
end;
run;

View solution in original post


All Replies
Solution
‎02-23-2017 10:02 AM
PROC Star
Posts: 308

Re: excluding 0 from after a decimal

Posted in reply to ysantosh18

Doe the "best." format not give you what you want?


data _null_;
do A = 1.0, 12.30, 11.07, 7.70, 0.0, 17.71;
put a 8.2 a best.;
output;
end;
run;

Occasional Contributor
Posts: 14

Re: excluding 0 from after a decimal

Posted in reply to collinelliot

 

It works absolutely fine

Thank you So much!

Valued Guide
Posts: 505

Re: excluding 0 from after a decimal

Posted in reply to ysantosh18
Removing leading and trailing 0s and blanks from a numeric variable(from display or char variable)

Be careful 'proc print' and 'put' can be decieving

HAVE
====

1.0
12.30
11.07
7.70
0.0
17.71

WANT
====

1
12.3
11.07
7.7
0
17.71

But I get when surrounding my put statement with '*'

*1*
*           1*
*           1*
*1           *


WANTED
======

*1* or just 1

WORKING CODE
============

    Thanks to data _null_ old post
    CutZro=substrn(a[i],1,max(5,a[i]));

FULL SOLUTION
=============

data chrCutZro;
  length CutZro $5;
  array A[6]  (1.0, 12.30, 11.07, 7.70, 0.0, 17.71);
  do i=1 to 6;
     CutZro=substrn(a[i],1,max(5,a[i]));
     put '*' CutZro +(-1) '*';
     put '*' a[i] best.  '*';
     leading_blanks=put(a[i],best.);
     put '*' leading_blanks $char12. '*';
     leading_blanks_left=put(a[i],best. -l);
     put '*' leading_blanks_left $char12. '*';
  end;
run;quit;


substrn   *1*
best.     *           1*
char12    *           1*
best -L   *1           *

substrn   *12.3*
best.     *        12.3*
char12    *        12.3*
best -L   *12.3        *

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 170 views
  • 1 like
  • 3 in conversation