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

Hi everyone,

How could I combine multiple proc univariate output datasets where each dataset is for a different BY (statement) variable?  I d like to have one combined data set where the first column is for the different levels of each BY variable.

 

Desired output:

                                    Mode Mean Min Mix

BY var1 - level 1

BY var 1 - level 2

BY var 2 - level 1

BY var 2 - level 2

BY var 2 - level 3

 

Current Output:

ByVar 1                            BYvar2                                 Mode Mean Min Mix

BY var1 - level 1                      .

BY var 1 - level 2                     .

.                                   BYvar2 - level 1

.                                   BYVar2  - level 2     

.                                   BYvar2 - level3           

 

 Current code:

%macro univariate(input, byvar, var, wgt, odata, num, nused);

proc sort data=&input; by &byvar; run;

proc univariate data=&input ;

var &var;

weight &wgt;

by &byvar;

output out=st_&var.&odata mean=Mean mode=Mode min=Minimum max=Maximum NMiss=Missing N=Ns;

run;

data st_&var.&odata;

set st_&var.&odata;

/*row=&num.;*/

/*row+1;*/

nused="&nused.";

N_=Ns+Missing;

rename N_=N;

drop missing Ns N_;

proc print data=st_&var.&odata;

run;

%mend;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You're probably best off cleaning it up in an additional data step using a coalesce function. You also don't need a macro if you use the CLASS and WAYS statement.


proc means data=sashelp.class stackods mode mean n min max;
class sex age;
ways 1;
var weight height;
ods output summary=want;
run;


data want;
length var $200.;
set want;
var=coalescec(sex, age);
drop sex age;
run;

 

View solution in original post

3 REPLIES 3
Reeza
Super User

You're probably best off cleaning it up in an additional data step using a coalesce function. You also don't need a macro if you use the CLASS and WAYS statement.


proc means data=sashelp.class stackods mode mean n min max;
class sex age;
ways 1;
var weight height;
ods output summary=want;
run;


data want;
length var $200.;
set want;
var=coalescec(sex, age);
drop sex age;
run;

 

Maisha_Huq
Quartz | Level 8

Thanks so much, Reeza - for also pointing out using proc means this way. Do you know if there's a way to specify what order to coalesce? In other words, can I specify a certain order for the by variables' valid values?

Reeza
Super User
Coalesce operates across the column, I assume your referring to the order of the rows?

The standard way is to use a format to control the order of the variable output.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1179 views
  • 1 like
  • 2 in conversation