BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6

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.000012.0000
                           Week1  MEAN 11.666711.7500
                           Week1  MIN  11.000011.0000
                           Week1  N     3.0000 4.0000
                           Week1  STD   0.5774 0.5000
                           Week2  MAX  14.000014.0000
                           Week2  MEAN 13.500013.6667
                           Week2  MIN  13.000013.0000
                           Week2  N     4.0000 3.0000
                           Week2  STD   0.5774 0.5774
                           Week3  MAX  15.000016.0000
                           Week3  MEAN 15.000015.3333
                           Week3  MIN  15.000015.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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

View solution in original post

7 REPLIES 7
Ksharp
Super User
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

michtka
Fluorite | Level 6

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;

Ksharp
Super User

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

michtka
Fluorite | Level 6

any sugestion,

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

Thanks.

Ksharp
Super User

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

michtka
Fluorite | Level 6

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.

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 7 replies
  • 1299 views
  • 6 likes
  • 2 in conversation