|
Rolland,
A still sleazier trick would be to write
by &by ;
SAS will accept empty BY statements. Some older verisons did not and
perhaps some procs still do not. So testing may be advised. Questions:
Should SAS be able to change this sort of behavior at whim from version to
version? Should sleazy programmers take advantage of this behavior? When
do we get a stable documented language? Perhaps someone could do a short
paper on the advantages of having a stable language.
717 data w ; do x = 1 to 5 ; output ; end ;
718 run ;
NOTE: The data set WORK.W has 5 observations and 1 variables.
NOTE: DATA statement used:
real time 0.04 seconds
725 data w ; do x = 1 to 5 ; output ; end ;
726 run ;
NOTE: The data set WORK.W has 5 observations and 1 variables.
NOTE: DATA statement used:
real time 0.05 seconds
727
728 data _null_ ;
729 set w ;
730 by ;
731 put _all_ ;
732 run ;
x=1 _ERROR_=0 _N_=1
x=2 _ERROR_=0 _N_=2
x=3 _ERROR_=0 _N_=3
x=4 _ERROR_=0 _N_=4
x=5 _ERROR_=0 _N_=5
NOTE: There were 5 observations read from the data set WORK.W.
NOTE: DATA statement used:
real time 0.00 seconds
IanWhitlock@westat.com
-----Original Message-----
From: Roland [mailto:roland@RASHLEIGH-BERRY.FSNET.CO.UK]
Sent: Wednesday, December 11, 2002 7:39 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: recruiting cheesy, sleasy SAS tricks
Here is my "byline trick". Suppose you have a by= parameter in your macro so
that the user can specify the "by" variables. If this is set then you will
probably need to add the line "by &by ;" underneath some of the procedure
calls. This is best handled by setting up a macro variable like in the
following:
%local byline;
%if %length(&by) %then %let byline=by &by %str(;);
and then you can add this line under your procedure calls as follows:
proc summary nway data=ds;
&byline
In this way you either get nothing if the by= parameter was not set or you
get the correct "by" line.
"Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
news:at7bfc$o6q$1@news7.svr.pol.co.uk...
> Another one I call the "global declaration trick". If you want to work out
> if a global macro variable has been set or not then declaring it as
global,
> if it already exists, will not affect its contents. So you can declare it
> global and then test its length to see if it has been set.
>
> And another one, a dummy macro declaration. This is a very cheesy and
sleazy
> trick. It is for when you want to run a macro once but never again during
a
> session. Sure, you are going to call it maybe dozens of times, but you do
> not want to run it if it has already been run. You could set up a global
> macro variable and test its setting and branch out of the macro, but that
> wouldn't be either cheesy or sleazy. So what you do instead (and I think
> this only works for members of an autocall library) is to put all the
macro
> code before the macro declaration. and just end the macro with:
>
> %macro mymacro;
> %mend mymacro;
>
> So all the macro code gets run and the stored macro contains nothing. That
> way you call it as many times as you like afterwards and it will do
nothing.
>
>
> "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> news:at7b0g$nsj$1@news7.svr.pol.co.uk...
> > Another one I have seen a lot and used to use a lot but not recently is
a
> > way of disabling a chunk of macro code within a larger macro. That is to
> use
> > something like:
> >
> > %macro dontdo;
> > ....
> > ...
> > ..
> > %mend dontdo;
> >
> > This is better that using comment lines since these may already exists
> > within the code you are trying to disable and so might terminate your
> > commentary section earlier than you thought.
> >
> >
> >
> > "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> > news:at7aqm$i0s$1@newsg3.svr.pol.co.uk...
> > > Another sleazy trick I use a lot is when I am assigning a value to a
> macro
> > > variable in proc sql. I use "separated by ' '" after it, not because I
> > have
> > > more than one item, but just to cut off the trailing blanks.
> > >
> > > "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> > > news:at719h$do0$1@newsg3.svr.pol.co.uk...
> > > > Another trick I use for when I want the results from some system
> > commands
> > > is
> > > > write the results out to a file and then read in that file and
assign
> it
> > > to
> > > > a macro variables using my %readfile macro. If changes the line
throws
> > to
> > > > singla spaces and it all comes out perfect and ready for further
> > > processing.
> > > >
> > > > http://mysite.freeserve.com/rolandrb/sasmacros/readfile.sas
> > > >
> > > >
> > > > Another sleazy trick is to ensure something is in quotes. To do this
I
> > > > compress for both single and double quaotes and enclose the whole
lot
> in
> > > > double quotes.
> > > >
> > > > %let file="%sysfunc(compress(&file,%str(%'%")))";
> > > >
> > > >
> > > > Another one which is actually quite sensible is for defaulting an
> output
> > > > dataset name. I scan for the first string up to a left parenthesis
> > whether
> > > > the parenthesis exists or not. That way I don't get the modifiers
that
> > > might
> > > > have been defined with the input dataset.
> > > >
> > > > %let dsout=%scan(&dsin,1,%str(%());
> > > >
> > > >
> > > > "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> > > > news:at70qi$lvh$1@news6.svr.pol.co.uk...
> > > > > Another one I use is "sum()=" in proc summary literally as
written.
> > This
> > > > > calculates the sum of all the numeric variables and puts the
result
> in
> > > > > variables of the same name. I'm not sure if this is common
practise.
> > > > >
> > > > > Another is to use the 0 or 1 for a boolean expression and part of
a
> > > > > calculation as in the following where I have subtracted 1 from a
> year
> > > > count
> > > > > but will add it on again if a condition is true.:
> > > > >
> > > > > %macro age(dob,date);
> > > > > year(&date)-year(&dob)-1+((month(&date)>month(&dob))
> > > > > or ((month(&date)=month(&dob)) and (day(&date)>=day(&dob))))
> > > > > %mend;
> > > > >
> > > > >
> > > > >
> > > > > "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> > > > > news:at6vs8$hss$1@newsg4.svr.pol.co.uk...
> > > > > > put (_all_) (=);
> > > > > >
> > > > > > ..is a good one to get sas to drop the _N_ and _ERROR_ automatic
> > > > variables
> > > > > > from the put _all_ list.
> > > > > >
> > > > > > Another good one is to trick sas into "flattening" a "proc
report"
> > > which
> > > > > is
> > > > > > to use a dummy "noprint" variable after an across variable.
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Michael L. Davis" <michael@BASSETTCONSULTING.COM> wrote in
> message
> > > > > >
> > > > >
> > > >
> > >
> >
>
news:20021210185607.GDAN17506.imta01a2.registeredsite.com@smtp.registeredsit
> > > > > > e.com...
> > > > > > > Hello Friends,
> > > > > > >
> > > > > > > I'd like to do a Coder's Corner presentation at NESUG next
year
> on
> > > the
> > > > > > subject of "Cheesy, Sleasy SAS Tricks". As an example, when
> coding
> > > SCL
> > > > > > programs, I'm in the habit of including the statement:
> > > > > > >
> > > > > > > rc=rc ;
> > > > > > >
> > > > > > > to suppress the compiler warning that I'm creating the
variable
> rc
> > > but
> > > > > not
> > > > > > using it.
> > > > > > >
> > > > > > > I'd welcome additional candidates for the role of "cheesy,
> sleasy
> > > > > tricks".
> > > > > > To qualify, they need to be coding declarations or other
> > constructions
> > > > > that
> > > > > > suppress the literal mindedness of SAS or its sometimes
> > > over-solicitous
> > > > > > manner of trying to protect us from ourselves.
> > > > > > >
> > > > > > > I'll be sure to acknowledge useful contributions, selected or
> > > > otherwise,
> > > > > > in the acknowledgement section of the presentation.
> > > > > > >
> > > > > > > BTW, thanks to all for the help last week with regards to
> > reordering
> > > > the
> > > > > > PDV. Unfortunately, the friend for whom I was trolling wanted
as
> > easy
> > > a
> > > > > > method as the RETAIN statement. The ATTRIB statement was to
> change
> > > the
> > > > > > length of a variable brought in by a SET statement. So
combining
> > the
> > > > > RETAIN
> > > > > > and ATTRIB statements did not work (the length was not changed
by
> > the
> > > > > ATTRIB
> > > > > > statement).
> > > > > > >
> > > > > > > - Michael "Mad Doggy" Davis
> > > > > > >
> > > > > > > Michael L. Davis
> > > > > > > Bassett Consulting Services, Inc.
> > > > > > > North Haven CT 06473-3712
> > > > > > > web: http://www.bassettconsulting.com
> > > > > > > email: michael@bassettconsulting.com
> > > > > > > tel: 203-562-0640
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
|