My data is like such:
My code is:
data contdem;
set contdem;
if _STAT_ in ('MEAN', 'MED') then do;
ALL= put(ALL, f6.3);
NOT_OBESE = put(NOT_OBESE, f6.3);
OBESE = put(OBESE, f6.3);
SEVERELY_OBESE = put(SEVERELY_OBESE, f6.3);
end;
else if _STAT_ = 'MIN' | _STAT_ = 'MAX' then do;
ALL= put(ALL, f5.2);
NOT_OBESE = put(NOT_OBESE, f5.2);
OBESE = put(OBESE, f5.2);
SEVERELY_OBESE = put(SEVERELY_OBESE, f5.2);
end;
run;
Perhaps I don't know what this format does, but I essentially want the mean and median to be always go out to 3 decimals and the minimum and maximum to always go out to 2 decimals. However, when I run this code, only some of the decimals go out to 3 or 2. For instance, max age 50 turns to 50.00 but max height 177.2 stays as 177.2 instead of 177.20.
Can someone help? Thank you in advance
Statements like this don't make much sense:
NOT_OBESE = put(NOT_OBESE, f6.3);
Is NOT_OBSESE a character variable or a numeric variable?
If it is character why are you using PUT() function to format it using a numeric format instead of a character format?
If it is numeric why are you assigning it a character string value?
This would make more sense:
NOT_OBESE_string = put(NOT_OBESE, f6.3);
SAS Formats are applied to Variables, not rows of values. One format per variable.
Since your requirement is per Statistic, then you cannot apply format per row and either have to 1) restructure the data set so that the variables that indicate the statistic are part of the column, or 2) settle for some approach that is applied at the time humans look at the values such as report text.
If you have a data set that is used to create that one you might be able to create report that reads the way you expect, maybe. Depends on what you really need.
Example using a data set you should have available to test code.
Proc tabulate data=sashelp.class; class sex; var age height weight; table (age height weight) *(mean*f=7.3 std*f=8.4 max*f=6.2 n ), all='All' sex=' ' ; run;
I hope you can see where the format is specified using the <statistic>*f=<format>. I did not supply a format for N to show the default behavior.
This procedure supports most of the common statistics (and a number of percentages).
For a great many purposes the format assigned to a numeric value makes no difference until people need to read it, which means a report, graph or the value in a table created by some procedure. Datasets, not so much.
Your "format" doesn't work because you are attempting to create Character values with Put into variables that are apparently numeric. SAS does maintain a difference between the two.
Did you read your log a see a lot conversion notes?
First, i have to repeat, what @ballardw already said:
SAS Formats are applied to Variables, not rows of values. One format per variable.
Depending on what you want to do with the dataset afterwards, there could be a way to achieve what you want, but only by converting all numeric variables to char. When exporting such data as pdf or csv, the data type hardly matters.
The following step is untested, because you haven't provided data in usable form.
data want;
set have(rename= (all = num_all /* add rename for other vars */));
length
format $ 32
all $ 10
;
select (_stat_);
when ('MEAN', 'MED') format = 'f6.3';
when ('MIN', 'MAX') format = 'f5.2';
otherwise format = ' ';
end;
if not missing(format) then do;
all = putn(num_all, format);
/* add other variables */
end;
else do;
all = cats(num_all);
end;
drop format num_:;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.