BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ysantosh18
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

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

3 REPLIES 3
collinelliot
Barite | Level 11

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;

ysantosh18
Obsidian | Level 7

 

It works absolutely fine

Thank you So much!

rogerjdeangelis
Barite | Level 11
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        *

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1025 views
  • 1 like
  • 3 in conversation