I'm trying to create a single field with the mean and standard deviation combined in mean(std) format. I have it mostly figured out, but the problem is that trailing zeros are being truncated. So rather than 27.00(18.70), it shows up as 27(18.7). But numbers such as 14.21(15.04) show up properly.
In the code below, the zeros are already truncated in the "test" dataset. How do I add them back in? The final numbers will be exported to an Excel table, so I don't think I could just apply a format to them.
data test;
input Mean Std;
datalines;
14.2123 15.0416
27.0000 18.7000
27.3185 19.8000
;
data want; set test;
StdRD = ROUND(Std,.01);
MeanRD = ROUND(Mean,.01);
Final_output = COMPRESS(MeanRD||"("||StdRD||")");
RUN;
Use the PUT function
Final_output = COMPRESS(put(MeanRD,8.2)||"("||put(StdRD,8.2)||")");
Use the PUT function
Final_output = COMPRESS(put(MeanRD,8.2)||"("||put(StdRD,8.2)||")");
Alternative solution using the VVALUE function
data want;
set test;
stdrd = round(std,.01);
meanrd = round(mean,.01);
format stdrd meanrd 8.2;
final_output = compress(vvalue(meanrd)||"("||vvalue(stdrd)||")");
run;
Anytime SAS converts a numeric value to character, such as to concatenate the values you attempt, it uses a BEST. format which doesn't show trailing zeroes. So you use a PUT and provide a desired format so you control the conversion.
You may find the -L option with the PUT function as in Put(meadrd,8.2 -L) useful to create values without any leading spaces that require another function to remove.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.