turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- median and range of a final dataset

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-11-2012 08:22 PM

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.0000 | 12.0000 | |

Week1 | MEAN | 11.6667 | 11.7500 | |

Week1 | MIN | 11.0000 | 11.0000 | |

Week1 | N | 3.0000 | 4.0000 | |

Week1 | STD | 0.5774 | 0.5000 | |

Week2 | MAX | 14.0000 | 14.0000 | |

Week2 | MEAN | 13.5000 | 13.6667 | |

Week2 | MIN | 13.0000 | 13.0000 | |

Week2 | N | 4.0000 | 3.0000 | |

Week2 | STD | 0.5774 | 0.5774 | |

Week3 | MAX | 15.0000 | 16.0000 | |

Week3 | MEAN | 15.0000 | 15.3333 | |

Week3 | MIN | 15.0000 | 15.0000 | |

Week3 | N | 2.0000 | 3.0000 | |

Week3 | STD | 0.0000 | 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;

Thanks in advance.

V.

Accepted Solutions

Solution

04-12-2012
12:57 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michtka

04-12-2012 12:57 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michtka

04-12-2012 12:57 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-12-2012 06:26 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michtka

04-12-2012 07:34 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-12-2012 07:42 AM

any sugestion,

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

Thanks.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michtka

04-12-2012 11:08 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-13-2012 06:22 AM

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

Many thanks in advance.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michtka

04-16-2012 04:38 AM

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