BookmarkSubscribeRSS Feed
Swordfish
Calcite | Level 5
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;
3 REPLIES 3
AllenEBingham
Calcite | Level 5
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;

ballardw
Super User
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;
Swordfish
Calcite | Level 5
Thanks to both Allen and Ballardw.This really helps.

Regards

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 691 views
  • 0 likes
  • 3 in conversation