Note that there are some explanatory texts on larger screens.

plurals
  1. POSAS proc univariate for many variables by macro or array
    text
    copied!<p>This should be an easy question, but I didn't figure out... I want to get mean and median of many variables by <code>proc univariate</code> as below. It's really time consuming to manually add <code>M_</code> for mean and <code>MD_</code> for median to all variables. I am wondering if there any simple approach, such as array to do so? Thanks a lot!</p> <p>Code:</p> <pre><code>data old; input year type A1 A2 A3 A4 A5; datalines; 2000 1 1 2 3 4 5 2000 1 2 3 4 5 6 2000 2 3 4 5 6 7 2000 2 4 5 6 7 8 2001 1 5 6 7 8 9 2001 1 6 7 8 9 10 2001 1 7 8 9 10 11 2001 2 8 9 10 11 12 2001 2 9 10 11 12 13 2001 2 10 11 12 13 14 2002 1 11 12 13 14 15 2002 1 12 13 14 15 16 2002 1 13 14 15 16 17 2002 2 14 15 16 17 18 2002 2 15 16 17 18 19 2002 2 16 17 18 19 20 run; proc univariate data=old noprint; var A1 A2 A3 A4 A5; by year type; output out=new mean=M_A1 M_A2 M_A3 M_A4 M_A5 median=MD_A1 MD_A2 MD_A3 MD_A4 MD_A5; run; </code></pre> <p>Expected demo Code:</p> <pre><code>%let varlist = A1 A2 A3 A4 A5; array vars (*) &amp;varlist; proc univariate data=old noprint; var &amp;vars(*); by year type; output out=new mean=M_&amp;vars(*) median=MD_&amp;vars(*); run; </code></pre> <p>correct code by using proc sql </p> <pre><code>%macro uni; %let varlist='A1','A2','A3','A4','A5'; %let vars=A1 A2 A3 A4 A5; proc sql; select cats('M_',name) into :meannamelist separated by ' ' from dictionary.columns where libname='WORK' and memname='OLD' and name in (&amp;varlist); select cats('MD_',name) into :mediannamelist separated by ' ' from dictionary.columns where libname='WORK' and memname='OLD' and name in (&amp;varlist); quit; proc univariate data=old; var &amp;vars; by year type; output out=new mean=&amp;meannamelist median=&amp;mediannamelist; run; %mend uni; options mprint; %uni; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload