Help using Base SAS procedures

can I use proc means more effeciently

Reply
Occasional Contributor
Posts: 18

can I use proc means more effeciently

Is there a better method to produce the output in the final step of the code given below. I do get the required output but it seems lengthy and inefficient.I believe the code is long and it definitley needs more sophistication and if I need to change into a macro how should it be dealt? Thanks
Data test;
input Married$ Divorced6$ BirthYear$ work;
Datalines;
Y N 92 15
Y N 93 15
Y N 94 5
Y N 95 6
Y N 93 7
Y Y 95 8
Y Y 92 9
Y Y 93 0
Y Y 94 9
Y Y 93 7
Y Y 93 3
Y Y 94 5
Y N 92 6
Y Y 94 7
Y Y 94 2
N Y 95 1
Y Y 95 3
Y Y 92 4
Y N 94 1
Y Y 93 1
Y Y 92 5
Y Y 92 5
Y Y 93 1
;
run;
data test1;
set test;
if Married='Y';
if Divorced6='N';
run;
proc means data=test1 Noprint;
class BirthYear;
var work;
output out=part1(Drop=_type_ _freq_)
N=count_No MEAN=Mean_No;
run;
data test2;
set test;
if Married='Y';
if Divorced6='Y';
run;
proc means data=test2 Noprint;
class BirthYear;
var work;
output out=part2(Drop=_type_ _freq_)
N=count_Yes MEAN=Mean_Yes;
run;
Proc sort data=part1;
by BirthYear;
run;
Proc sort data=Part2;
by BirthYear;
run;
Data combine1;
merge part1 part2;
by BirthYear;
run;
proc sort data=test1;
by BirthYear;
run;
proc sort data=test2;
by BirthYear;
run;
data together;
set test1 test2;
by BirthYear;
run;
proc means data=together;
class BirthYear;
var work;
output out=part3(Drop=_type_ _freq_)
N=totalcount_Yes MEAN=totalMean_Yes;
run;
Data final;
merge combine1 part3;
by BirthYear;
run;
Data separate;
set final;
if _N_ =1 then output;
run;
Data final1;
set final separate;
if _N_ =1 then delete;
run;
Data final1 ;
set final1;
if BirthYear='' then BirthYear='total' ;
else BirthYear=BirthYear ;
run;
Proc print data=final1;
run;
Contributor
Posts: 22

Re: can I use proc means more effeciently

I'm not sure if this really is all that more efficient, but it results in the same (I didn't repeat your first data step ... but you need that to get this started):



proc means data=test noprint;
where Married eq 'Y';

   class Married Divorced6 BirthYear;

   var work;

   output out=test_sum(drop=_type_ _freq_)

            n=n

         mean=mean;

run;

data test_sum_needed;

   set test_sum;

   where Married ne ' ';


   if BirthYear eq ' ' then BirthYear='total'; /* total sorts after years */

   else;

   if Divorced6 eq ' ' then Divorced6='Z';     /* Z sorts after Y and N */

   else;


run;


proc sort data=test_sum_needed;

   by BirthYear Married Divorced6;

run;


data test_final1;

   set test_sum_needed;

   by BirthYear;



   retain count_No Mean_NO count_Yes Mean_Yes totalcount_Yes totalMean_Yes;

   array rest(6) count_No Mean_NO count_Yes Mean_Yes totalcount_Yes
totalMean_Yes;



   if first.BirthYear then do i = 1 to 6 by 1;

      rest(i) = .;

   end;

   else;



   if Divorced6 eq 'N' then do;

      count_No = n;

      Mean_No = mean;

   end;

   else if Divorced6 eq 'Y' then do;

      count_Yes = n;

      Mean_Yes = mean;

   end;

   else if Divorced6 = 'Z' then do;

      totalcount_Yes = n;

      totalMean_Yes = mean;

   end;

   else;



   if last.BirthYear then output;

   else;



   drop Married Divorced6 n mean i;

run;


proc print data=test_final1;

run;

Super User
Posts: 10,550

Re: can I use proc means more effeciently

If I were more interested in the results than the particular Proc I'd try without any other manipulation of the Test data set.

proc tabulate data=test;
class birthyear married divorced6;
var work;
table birthyear all='Total', ( divorced6 married) *work='' *(n='Count' mean*f=f8.4);
run;
Occasional Contributor
Posts: 18

Re: can I use proc means more effeciently

Thanks to both Allen and Ballardw.This really helps.

Regards
Ask a Question
Discussion stats
  • 3 replies
  • 103 views
  • 0 likes
  • 3 in conversation