Formatting macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

Formatting macro variables

How can I format the following macro variable so that it can display decimals? Right now it only displays 100 and I want to to display for instance 100.34.

%let add_coll=100;

Thanks!


Accepted Solutions
Solution
‎10-23-2012 12:56 PM
Super User
Posts: 17,832

Re: Formatting macro variables

I'm guessing that your while component is also a character comparison so you need to make it numeric as well.

It also doesn't call a macro variable, just the variable add_col1 rather than &add_col1

I wonder if you could do what you needed via a data _null_ step instead but anyways...

%macro run_this;

%let add_col=5;

%do %while (%sysevalf(%sysfunc(putn(&add_col, 8.2)) <100));

    %let add_col=%sysevalf(&add_col+0.5);

    %put &add_col;

%end;

%mend;

%run_this;

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Formatting macro variables

1    %let add_coll=%sysfunc(putn(100.34,6.2));

2    %put &add_coll;

100.34

Super User
Posts: 17,832

Re: Formatting macro variables

You can use the putn function as Linlin has suggested, but do remember all macro variables are character and not numeric.

%let add_coll=%sysfunc(putn(100.34,6.2));

%let add_col2=100.34;

%let add_col3=%sysfunc(putn(100, 6.2));

%put 1: &add_coll  2: &add_col2 3: &add_col3;

Frequent Contributor
Posts: 121

Re: Formatting macro variables

I also need to display decomals for a macro variable that has a value of 165000128. So if %let add_coll=165000128, how do I make it so that I can see 165000218.whatever, whatever being two decimal spaces.

Super User
Posts: 17,832

Re: Formatting macro variables

How are you assigning and outputting these macro variables?

You can use the putn like Linlin mentioned above, but like I said before a macro variable is a CHARACTER value not a number so you can't apply numeric formats.

There are ways around this when you use it, but you need to provide more information on what you're trying to do.

Frequent Contributor
Posts: 121

Re: Formatting macro variables

I think I understand what you are saying now; I am assigning the macro variable in question (add_coll), by using a standard %let statement. Add_coll takes on values from 0 to 200,000,000 and once it reaches a level of 150,000,000, I am trying to increment it by 0.01. This being the case, I am trying to format add_coll such that it takes in decimals. Below is what I've been trying to run.

%macro run;

    %let add_coll=150,000,000;

    %do %while (add_coll<200,000,000);

    %let add_coll=%sysevalf(&add_coll + 0.01);

%mend;


Solution
‎10-23-2012 12:56 PM
Super User
Posts: 17,832

Re: Formatting macro variables

I'm guessing that your while component is also a character comparison so you need to make it numeric as well.

It also doesn't call a macro variable, just the variable add_col1 rather than &add_col1

I wonder if you could do what you needed via a data _null_ step instead but anyways...

%macro run_this;

%let add_col=5;

%do %while (%sysevalf(%sysfunc(putn(&add_col, 8.2)) <100));

    %let add_col=%sysevalf(&add_col+0.5);

    %put &add_col;

%end;

%mend;

%run_this;

Frequent Contributor
Posts: 121

Re: Formatting macro variables

Thanks!

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 280 views
  • 4 likes
  • 3 in conversation