DATA Step, Macro, Functions and more

Formatting decimal points after using Cat function

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

Formatting decimal points after using Cat function

Hi everyone,

 

I have a mean, lower CI variable and upper CI variable. I would like the combine them into one variable. The issue is the CI variable has a long decimal trail. I would like to limit it to two decimals after using the Cat function. So here is my code currently:

 

data have;
input Mean  :  lowerCLmean : upperCLmean;
cards;
45               30.123456                         50.789123
20               15.789123                         30.123456

;

data want;
set have;
new=cat(mean,' (',lowerclmean,'-',upperclmean,')');

proc print;run;
 
What I really want:

Obs Mean lowerCLmean upperCLmean Really Want
22015.789130.123520 (15.78-30.12)

 

 

Thanks in advance! 


Accepted Solutions
Solution
‎03-28-2018 01:40 PM
Super User
Posts: 13,293

Re: Formatting decimal points after using Cat function

data want;
set have;
new=cat(mean,' (',put(lowerclmean,f5.2 -L),'-',put(upperclmean,F5.2 -L),')');

 

You can nest all sorts of functions inside the cat, cats, catt, catx etc. functions as long as the results are strings.

View solution in original post


All Replies
Solution
‎03-28-2018 01:40 PM
Super User
Posts: 13,293

Re: Formatting decimal points after using Cat function

data want;
set have;
new=cat(mean,' (',put(lowerclmean,f5.2 -L),'-',put(upperclmean,F5.2 -L),')');

 

You can nest all sorts of functions inside the cat, cats, catt, catx etc. functions as long as the results are strings.

Contributor
Posts: 60

Re: Formatting decimal points after using Cat function

Thank you Ballard! Can you tell me what the  "-L" does?

Super User
Posts: 13,293

Re: Formatting decimal points after using Cat function

The -L left justifies the result. if your value were actually 9.33 without the -L there would be a leading space as the F5.2 format will display the result in 5 columns and right justified by default: " 9.33" . Since it appeared that you did not want the CI to look like ( 9.33-12.75) for example I used that option.

 

Character formats will default to left justification but if you want right justification when using Put with a character format you could use put(somevar,$charfmt. -R) which may appear better for some table row headers or graph axis values.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 103 views
  • 2 likes
  • 2 in conversation