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

How can I display my output results with commas?

 

%let one_hummingbird = 3.5; /* grams */
%let elephant_weight = 8800; /* pounds */

%let pound_birds = %sysfunc(floor(%sysevalf((16*28.3495231)/&one_hummingbird.)));
/* average hummingbirds weight is 3 to 4 grams 
   28.3495231 grams = 1 ounce
   16 ounces to 1 pound
   so 453.59237 grams = 1 pound
   129 hummingbirds to lift 1 pound where the birds weight is 3.5 grams
    and the hummingbird can lift it's self plus it's weight again
   
   African elephants weigh between 4,000-7,000 kg (8,800-15,400 lb).
   Asian elephants weigh between 3,000-5,000 kg (6,600-11,000 lb).
 
*/
%let num_birds_needed = %sysfunc(floor(%sysevalf( 453.59237/&one_hummingbird.*&elephant_weight.)));

data have;
sentence = "With an elephant that weights &elephant_weight. pounds 
     and hummingbirds able to carry an additional &one_hummingbird. grams, 
     then you need &num_birds_needed. hummingbirds.";
run;

proc print data=have;
run;
Obs sentence
1 With an elephant that weights 8800 pounds and hummingbirds able to carry an additional 3.5 grams, then you need 1140460 hummingbirds.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Using comma10. format

 

 

%let num_birds_needed = %sysfunc(floor(%sysevalf( 453.59237/&one_hummingbird.*&elephant_weight.)),comma10.);
%put &num_birds_needed;

 

 

 


%let one_hummingbird = 3.5; /* grams */
%let elephant_weight = 8800; /* pounds */

%let pound_birds = %sysfunc(floor(%sysevalf((16*28.3495231)/&one_hummingbird.)),comma10.);
%put &pound_birds;
/* average hummingbirds weight is 3 to 4 grams 
   28.3495231 grams = 1 ounce
   16 ounces to 1 pound
   so 453.59237 grams = 1 pound
   129 hummingbirds to lift 1 pound where the birds weight is 3.5 grams
    and the hummingbird can lift it's self plus it's weight again
   
   African elephants weigh between 4,000-7,000 kg (8,800-15,400 lb).
   Asian elephants weigh between 3,000-5,000 kg (6,600-11,000 lb).
 
*/
%let num_birds_needed = %sysfunc(floor(%sysevalf( 453.59237/&one_hummingbird.*&elephant_weight.)),comma10.);
%put &num_birds_needed;

data have;
sentence = "With an elephant that weights &elephant_weight. pounds 
     and hummingbirds able to carry an additional &one_hummingbird. grams, 
     then you need &num_birds_needed. hummingbirds.";
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@VDD wrote:

How can I display my output results with commas?

 

Obs sentence
1 With an elephant that weights 8800 pounds and hummingbirds able to carry an additional 3.5 grams, then you need 1140460 hummingbirds.

 


It seems like your output does have a comma, so I'm not understanding the problem.

--
Paige Miller
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

the results for the 3 macros are numeric and need comma's for readability.

8800 pounds should need to be displayed as 8,800 pounds.  The same of the number of birds.  Where 1140460 needs to display as 1,140,460.

 

novinosrin
Tourmaline | Level 20

Using comma10. format

 

 

%let num_birds_needed = %sysfunc(floor(%sysevalf( 453.59237/&one_hummingbird.*&elephant_weight.)),comma10.);
%put &num_birds_needed;

 

 

 


%let one_hummingbird = 3.5; /* grams */
%let elephant_weight = 8800; /* pounds */

%let pound_birds = %sysfunc(floor(%sysevalf((16*28.3495231)/&one_hummingbird.)),comma10.);
%put &pound_birds;
/* average hummingbirds weight is 3 to 4 grams 
   28.3495231 grams = 1 ounce
   16 ounces to 1 pound
   so 453.59237 grams = 1 pound
   129 hummingbirds to lift 1 pound where the birds weight is 3.5 grams
    and the hummingbird can lift it's self plus it's weight again
   
   African elephants weigh between 4,000-7,000 kg (8,800-15,400 lb).
   Asian elephants weigh between 3,000-5,000 kg (6,600-11,000 lb).
 
*/
%let num_birds_needed = %sysfunc(floor(%sysevalf( 453.59237/&one_hummingbird.*&elephant_weight.)),comma10.);
%put &num_birds_needed;

data have;
sentence = "With an elephant that weights &elephant_weight. pounds 
     and hummingbirds able to carry an additional &one_hummingbird. grams, 
     then you need &num_birds_needed. hummingbirds.";
run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

thank you @novinosrin that works.  I see that the formatting needed to be done in the assignment statement of the %let.

PaigeMiller
Diamond | Level 26

I see that the formatting needed to be done in the assignment statement of the %let.

 

The formatting can be done in the %LET statement. It can also be done inside the line that begins with SENTENCE= .

 

Or this whole thing might be done easier in a DATA _NULL_ step rather than using macro variables.

--
Paige Miller
ballardw
Super User

@PaigeMiller wrote:

I see that the formatting needed to be done in the assignment statement of the %let.

 

The formatting can be done in the %LET statement. It can also be done inside the line that begins with SENTENCE= .

 

Or this whole thing might be done easier in a DATA _NULL_ step rather than using macro variables.


Old school without creating a long variable:

data _null_;
   elephant_weight= 8800;
   one_hummingbird = 3.5;
   num_birds_needed =(453.59237/one_hummingbird) * elephant_weight;
   put "With an elephant that weighs"+1 elephant_weight comma5. +1"pounds and hummingbirds
 able to carry an additional"+1 one_hummingbird +1"grams, then you need" +1 num_birds_needed comma10.
 +1 "hummingbirds.";
run;

Or building the string with a concatenation function

 

data _null_;
   elephant_weight= 8800;
   one_hummingbird = 3.5;
   num_birds_needed =(453.59237/one_hummingbird) * elephant_weight;
   string = catx(' ',"With an elephant that weighs",put(elephant_weight, comma5.),"pounds and hummingbirds
 able to carry an additional",one_hummingbird,"grams, then you need",put(num_birds_needed, comma10.),
"hummingbirds.");
   put string;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1692 views
  • 1 like
  • 4 in conversation