## median and range of a final dataset

Solved
Super Contributor
Posts: 301

# median and range of a final dataset

Hi everyone, if you run the below piece of code, you will obtain the next dataset *:

Please can someone help me to calculate a final dataset including the median and range to this dataset * using the below piece of code, resulting something like that:

cpevent _stat_ f m

week1    max

week1    mean

week1    n

week1    std

week1    median

week1    range

and so on...

dataset *

--------------------

 cpevent _STAT_ F M

 Week1 MAX 12 12 Week1 MEAN 11.6667 11.75 Week1 MIN 11 11 Week1 N 3 4 Week1 STD 0.5774 0.5 Week2 MAX 14 14 Week2 MEAN 13.5 13.6667 Week2 MIN 13 13 Week2 N 4 3 Week2 STD 0.5774 0.5774 Week3 MAX 15 16 Week3 MEAN 15 15.3333 Week3 MIN 15 15 Week3 N 2 3 Week3 STD 0 0.5774

piece of code:

-------------------

data new (keep=cpevent sex age);

set sashelp.class;

if 12 => age then cpevent='Week1';

else if 12 < age < 15 then cpevent='Week2';

else cpevent='Week3';

run;

proc sort data=new out=new2;

by cpevent sex;

run;

proc means data=new2 noprint;

by cpevent sex;

var age;

output out=new3 (drop=_type_ _freq_);

run;

proc sort data=new3 out=new4;

by cpevent _stat_;

run;

proc transpose data=new4 out=new5 (drop=_name_);

by cpevent _stat_;

id sex;

var age;

run;

V.

Accepted Solutions
Solution
‎04-12-2012 12:57 AM
Super User
Posts: 10,770

## Re: median and range of a final dataset

```data new (keep=cpevent sex age);

set sashelp.class;

if 12 => age then cpevent='Week1';

else if 12 < age < 15 then cpevent='Week2';

else cpevent='Week3';

run;

proc sort data=new out=new2;

by cpevent sex;

run;

proc means data=new2 noprint ;

by cpevent sex;

var age;

output out=new3(drop=_type_ _freq_) n=n min=min max=max mean=mean std=std median=median range=range;

run;

proc transpose data=new3 out=new4 ;

by cpevent ;

id sex;

var n min max mean std median range;

run;
```

Ksharp

All Replies
Solution
‎04-12-2012 12:57 AM
Super User
Posts: 10,770

## Re: median and range of a final dataset

```data new (keep=cpevent sex age);

set sashelp.class;

if 12 => age then cpevent='Week1';

else if 12 < age < 15 then cpevent='Week2';

else cpevent='Week3';

run;

proc sort data=new out=new2;

by cpevent sex;

run;

proc means data=new2 noprint ;

by cpevent sex;

var age;

output out=new3(drop=_type_ _freq_) n=n min=min max=max mean=mean std=std median=median range=range;

run;

proc transpose data=new3 out=new4 ;

by cpevent ;

id sex;

var n min max mean std median range;

run;
```

Ksharp

Super Contributor
Posts: 301

## Re: median and range of a final dataset

Thanks Ksharp, it works.

Another weird problem, when I format the variable mean in proc means, when i make proc transpose,

the mean dont retain the format before.

do you know Why?

proc means data=newss noprin;
var vsbasech;
by vstext visit cpevent treattxt;
output out=newssmed n=n mean=mean std=std median=median min=min max=max;
format mean 6.1;
run;

proc sort data=newssmed out=newssmeds;
by visit cpevent;
run;

proc transpose data=newssmeds out=newssmedst (drop=_label_);
by visit cpevent;
id vstext treattxt;
var n mean std median min max;
run;

Super User
Posts: 10,770

## Re: median and range of a final dataset

Yes.They are two different columns before and after transposing, So you cann't retain the format any more.

Maybe you can transform MEAN into a Character value to hold the form of format.

Ksharp

Super Contributor
Posts: 301

## Re: median and range of a final dataset

any sugestion,

you mean convert the numeric mean to character before the transposition?

Thanks.

Super User
Posts: 10,770

## Re: median and range of a final dataset

OK. Here is an example. You can get whatever format after transposing dataset.Assuming I want STD have a PERCENT format.

```data new (keep=cpevent sex age);
set sashelp.class;
if 12 => age then cpevent='Week1';
else if 12 < age < 15 then cpevent='Week2';
else cpevent='Week3';
run;
proc sort data=new out=new2;
by cpevent sex;
run;
proc means data=new2 noprint ;
by cpevent sex;
var age;
output out=new3(drop=_type_ _freq_) n=n min=min max=max mean=mean std=std median=median range=range;
run;

proc transpose data=new3 out=new4 ;
by cpevent ;
id sex;
var n min max mean std median range;
run;

data new5(drop=F M);
set new4;
if _name_ eq 'std' then do;_F=put(F,percent8.2); _M=put(M,percent8.2); end;
else do;_F=put(F,best8.2); _M=put(M,best8.2); end;
run;

```

Ksharp

Super Contributor
Posts: 301

## Re: median and range of a final dataset

Thanks Ksharp, I like

what about (been told by data_null):

...

proc means data=new2 noprint ;

by cpevent sex;

var age;

output out=new3(drop=_type_ _freq_) n=n min=min max=max mean=mean std=std median=median range=range;

format n 3. min 3. max 3. mean 6.1 std 6.2 media 3. range 3.;

run;

proc transpose data=new3 out=new4  (where=_name_ ne 'CPEVENT' );

by cpevent ;

id sex;

var n min max mean std median range cpevent; /* adding cpevent to change the variables (F,M) from numeric to character */

run;

Another 2 questions in the way of optimizing my code...

1. What do you think of the idea to use class rather than by in the proc means?

I know that using class instead of by and ridding off a previous proc sorting is a bit tentative...but any sugestion in favour of ussing by rather than class?

2. Which is the difference between proc means of proc summary nway

Super User
Posts: 10,770

## Re: median and range of a final dataset

En......

1) As you said, CLASS don't need to pre-sort. But BY is more efficient than CLASS, especially when you have a larget dataset.

2)There is no little different between MEANS and SUMMARY. SUMMARY is usually used when you only want to generate a output dataset. MEANS is usually to display the output in the LOG, if you only need to see the result ,then MEANS is a good choice.

Ksharp

🔒 This topic is solved and locked.