I'm trying to create output for means and standard deviations that can easily be moved into a table. The final output should be in the form 3.20 (1.11). The code I have is pretty close, but it's dropping trailing zeros. The example I just provided actually shows up as 3.2 (1.11). How do I make sure both the mean and the standard deviation have exactly 2 numbers after the decimal point?
I found another post that said something about using the format f12.2, but I couldn't get that to work.
PROC MEANS data=SAShelp.cars mean stddev;
var enginesize;
OUTPUT OUT=carsengine;
RUN;
PROC TRANSPOSE data = carsengine out = carsengine_trp;
by _TYPE_;
id _STAT_;
var enginesize;
RUN;
DATA want; SET carsengine_trp;
mean_rd = round(Mean, .01);
std_rd = round(std, .01);
/*mean_SD = cat(input(mean_rd f12.2),'(',(std_rd f12.2),')');*/ /*This doesn't work*/
mean_SD = cat(mean_rd,' (',std_rd,')');
RUN;
PROC PRINT data=want; RUN;
DATA want;
SET carsengine_trp;
mean_SD = cat(put(mean,12.2),' (',trim(put(std,12.2 -L)),')');
RUN;
If you are not required to create values like 3.20 (1.11) because of some government agency requiring it, you would be much better off putting mean and standard deviation in seperate columns, it is much easier, no transpose or concatenation required.
If you are required to produce a lot of tables with this need to create values like 3.20 (1.11), perhaps you should learn the %TABLEN macro, which has all of this built in, you don't have to create the code yourself.
DATA want;
SET carsengine_trp;
mean_SD = cat(put(mean,12.2),' (',trim(put(std,12.2 -L)),')');
RUN;
If you are not required to create values like 3.20 (1.11) because of some government agency requiring it, you would be much better off putting mean and standard deviation in seperate columns, it is much easier, no transpose or concatenation required.
If you are required to produce a lot of tables with this need to create values like 3.20 (1.11), perhaps you should learn the %TABLEN macro, which has all of this built in, you don't have to create the code yourself.
Perfect, thanks! Can I ask what the -L option does?
Easy way to find out: try it without -L and you will see.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.