Date: Wed, 22 Nov 2006 11:12:30 -0500
Reply-To: Jack Clark <JClark@CHPDM.UMBC.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jack Clark <JClark@CHPDM.UMBC.EDU>
Subject: Re: PROC FREQ
Content-Type: text/plain; charset="iso-8859-1"
Toby,
I learned something new. Your reply to this question with the suggestion to
use the colon modifier is much nicer.
Thanks.
Jack Clark
Research Analyst
Center for Health Program Development and Management
University of Maryland, Baltimore County
-----Original Message-----
From: toby dunn [mailto:tobydunn@hotmail.com]
Sent: Wednesday, November 22, 2006 10:55 AM
To: JClark@CHPDM.UMBC.EDU; SAS-L@LISTSERV.UGA.EDU
Subject: Re: PROC FREQ
Jack ,
I am a little perplexed (which isnt too unussual in my world) as top why you
choose to go the macro route.
So let us explore the world of generating different lists of values for Proc
Freqs Table statement:
Well for starters one has several possibilities:
1.) Only Numeric
2.) Only Character
3.) All Variables
4.) A Selected List
5.) All Vars Starting with a Certain Prefix
6.) All Vars Ending with a Certain Suffix
1.) Table _Numeric_
2.) Table _Character_
3.) Table _All_
4.) <User supplied list>
5.) <Prefix>: <----Notice the use of the Colon modifier here
6.) Special case here, since SAS doesnt have a nice handy slick and easy way
of retrieving or selecting variables that end in a certain suffix one has to
sally forth and create a way. Well either that or you have to type all
those vars out and who wants to do that. So now one has to ask themselves
what is the problem I need to solve and how do I want to solve it. Well
conviently enough I have ask this very questin before. All of the macros
which did this at the time simply ran a proc contenst or SQL query and
retrieved these then loaded them into a macro var and used that. Well
seemed overlycomplex for me and frankly just looked wierd to me. I mean
shouldnt the macro be used in context. Well probably but that would mean
that the list of var names would have to be generated outside of data step
or procedure. So to that end I headed into the unknown and came up with
this:
%macro GetVarsEndingWith( DataIn = , Suffix = ) ;
%Local Dsid NumOfVars VarList VarName Close I ;
%Let Dsid = %Sysfunc( Open ( &DataIn , IS ) ) ;
%Let NumOfVars = %Sysfunc( Attrn( &Dsid , Nvars ) ) ;
%Do I = 1 %To &NumOfVars ;
%Let VarName = %Sysfunc( VarName( &Dsid , &I ) ) ;
%If ( %Substr( %SysFunc( Reverse(&VarName) ) , 1 , %Length(&Suffix) ) =
%SysFunc( Reverse(&Suffix) ) ) %Then %Do ;
%Let VarList = &VarList &VarName ;
%End ;
%End ;
%Let Close = %Sysfunc( Close( &dsid ) ) ;
&VarList
%Mend GetVarsEndingWith ;
Which means I can do something Like:
Data One ;
AXXX = 1 ;
BXXX = 1 ;
CXZX = 1 ;
AZZX = 1 ;
AXZZ = 1 ;
CXXX = 1 ;
Run ;
Proc Print
Data = One ;
Var %GetVarsEndingWith( DataIn = One , Suffix = XXX ) ;
Run ;
And the whole thing seems pretty darn clean and easy. And i am using and
generating the list right where I need it, rather than before I need it.
Toby Dunn
Quickly, bring me a beaker of wine, so that I may wet my mind and say
something clever.
Aristophanes
Wise people, even though all laws were abolished, would still lead the same
life.
Aristophanes
You should not decide until you have heard what both have to say.
Aristophanes
From: Jack Clark <JClark@CHPDM.UMBC.EDU>
Reply-To: Jack Clark <JClark@CHPDM.UMBC.EDU>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: PROC FREQ
Date: Wed, 22 Nov 2006 10:34:28 -0500
This is one approach using the SASHELP.VCOLUMN table.
* create sample data ;
data dat1;
input disp3_1 $ disp3_2 $ disp3_4 $;
cards;
A B C
D E F
G H I
;
run;
* create sample data ;
data dat2;
input disp3_1 $ disp3_3 $ disp3_5 $;
cards;
J K L
M N O
P Q R
;
run;
* provide the dataset name as a macro parameter ;
%macro getdisp (dsname);
proc sql noprint;
select name
into :vnames separated by ' '
from sashelp.vcolumn
where libname = "WORK" and
memname = upcase("&dsname")
;
quit;
%put &vnames;
title1 "Frequencies for &dsname Dataset";
proc freq data = &dsname;
tables &vnames;
run;
%mend getdisp;
%getdisp (dat1);
%getdisp (dat2);
Jack Clark
Research Analyst
Center for Health Program Development and Management
University of Maryland, Baltimore County
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
priyaworld2@YAHOO.COM
Sent: Wednesday, November 22, 2006 9:08 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: PROC FREQ
Hi All,
While writing a PROC FREQ statement, in the TABLES statement can we
specify variables like starting with something option.
For eg, in a dataset I have variables like DISP3_1 DISP3_2 DISP3_4
DISP3_6. Because of the discontinuity in the numbering, the below code
using do loop gives me an error:
do i = 1 to 6;
proc freq data = dat1;
tables DISP&i;
run;
ERROR: DISP3_3 NOT FOUND. DISP3_5 NOT FOUND.
This procedure has to be run for different datasets where we have
different DISP variables.So, there is not a fixed pattern for these
varibles.( dat2 may have DISP4_1 DISP4_2 DISP4_3 DISP4_5 etc...)
Can we write a code where we can specify the option to get all
variables starting with "DISP"?
Kindly suggest a way.
_________________________________________________________________
All-in-one security and maintenance for your PC. Get a free 90-day trial!
http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://clk
.atdmt.com/MSN/go/msnnkwlo0050000001msn/direct/01/?href=http://www.windowson
ecare.com/?sc_cid=msn_hotmail