BookmarkSubscribeRSS Feed
ChrisWoo
Obsidian | Level 7

Hi guys, would like to ask why i cant convert numeric to character with decimal places of 2 ?

i use the wrong format ? 

 

Numeric value = 0.250000

Value (character) i want = 0.25

 

extract from my code:

put(from_numeric_value,20.6)

 

 

ChrisWoo_0-1728986026531.png

%let sdis6_decimal_0 = "loanpurpose","original_tenure","principal_repay_term","specific_dfi_financing_details","startup_financing","refinance";
%let sdis6_decimal_2 = "interest_rate","rebate_rate","value_of_asset_purchase","dsr","refinance_value","eir","pod_original";

 data work.AuditLog_Mart_SDIS6 (/*drop=from_numeric_value to_numeric_value*/
           rename=(update_field=Variable)
           compress=yes
          );
  set prccris.ccris_audit_log;
  where sdis_filename = "SDIS6" and position_dt="&position_dt"d;



  /* Note: Transform numeric to string and remove blanks space*/
  if update_field in (&sdis6_decimal_0) then do;
   from_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,20.0)));
   to_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,20.0)));
   deci_tag = "0 deci";
  end;

  else if update_field in (&sdis6_decimal_2) then do;
   from_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,20.6)));
   to_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,20.6)));
   deci_tag = "2 deci";
  end;

  from_character_value = strip(from_character_value);
  to_character_value = strip(to_character_value);
 run;
3 REPLIES 3
Patrick
Opal | Level 21

I assume the root cause of the missing values is that your conditions are never true.

 

if update_field in (&sdis6_decimal_0) then

 

 You could easily test this by adding another else condition like:

else to_numeric_value_c ='9999';

 

Given the info you've shared you want your character variables populated wth full precision but without trailing zeros. Format BESTn. does this for you.

I assume you've used ifc() for missings to become blanks. You could create a custom format instead. See below code for further explanation.

proc format;
  value myBest(default=16)
    . = ' '
    other=[best16.]
    ;
run;

data test;
  input from_numeric_value;
  /* your syntax with format best32. */
  to_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,best16.)));
  /* alternative syntax returning the same result */
  to_numeric_value_c2=put(from_numeric_value,myBest.);
  datalines;
0.25
0
.
0.0025
;
run;

proc print data=test;
run;

proc contents data=test;
run;

 

Patrick_0-1728988574790.png

Also note that if using ifc() you should use a length statement to avoid a wasteful length of the created character variable.

Patrick_1-1728988719554.png

 

....and just reading what @Kurt_Bremser wrote: Yes, of course, as this is likely for reporting just use the BESTn. or the custom format directly on the numerical variable. And there is also the missing option where you can change the default behaviour of printing missings as a dot.

options missing=' ';

 

Kurt_Bremser
Super User

What is the purpose of storing a number as character? If you want specific formats depending on some value for reporting, you can do that in PROC REPORT. For anything other than reporting, keeping the number as such is the way to go.

To get only two decimals, you must say so in the format; use 20.2 instead of 20.6.

Amir
PROC Star

Hi @ChrisWoo,

 

Should the following line of code (seems to appear twice):

 

to_numeric_value_c = strip(ifc(missing(from_numeric_value),"",put(from_numeric_value,20.6)));

be (test / convert "to_" variable):

 

to_numeric_value_c = strip(ifc(missing(to_numeric_value),"",put(to_numeric_value,20.6)));

 

If not, then please clarify which variable in the data set you expect to have the non-zero character value.

 

 

Thanks & kind regards,

Amir.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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