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.
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
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
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;
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
any sugestion,
you mean convert the numeric mean to character before the transposition?
Thanks.
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
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.