BookmarkSubscribeRSS Feed
mariko5797
Pyrite | Level 9

My data is like such:

mariko5797_0-1628712293316.png

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

3 REPLIES 3
Tom
Super User Tom
Super User

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);
ballardw
Super User

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?

 

andreas_lds
Jade | Level 19

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1968 views
  • 1 like
  • 4 in conversation