Date: Tue, 3 Dec 2002 11:32:14 +0100
Reply-To: Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Subject: Re: Macro query (quite long - sorry)
Content-Type: text/plain; charset="utf-8"
Hi David,
It is my impression that you are making it more complicated than necessary.
First of all, changing characters in macro values in macro code can simply
be performed using %SYSFUNC(TRANSLATE(...,...,...)). Secondly, passing
argument values with embedded special characters, like commas, to parameters
may be done by macro quoting the whole value, i.e.
%XPANDF(%STR(4201,4300-4309/5120-5129))
So, without looking further in the reasons for the unexpected results, try
to restart from here and see what happens. HTH.
Regards - Jim.
--
Y. (Jim) Groeneveld, MSc IMRO TRAMARKO tel. +31 412 407 070
senior statist./data man. P.O. Box 1 fax. +31 412 407 080
J.Groeneveld@ITGroups.com 5350 AA BERGHEM, NL www.imrotramarko.com
My computer does what I tell it to do; sometimes it says my instructions are
illegal.
Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.
> -----Original Message-----
> From: David Yeates [mailto:david.yeates@UHCE.OX.AC.UK]
> Sent: 02 December 2002 17:09
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Macro query (quite long - sorry)
>
>
> Hi All,
>
> I have a requirement for passing parameters via a macro call
> which I then
> use
> in a format procedure call to define groupings on a dataset variable.
>
> The problem is the parameter itseld may contain a comma (,).
>
> I have got round this by passing the parameter with comms
> replaced by plus
> signs (+),
> and replacing them in a macro which builds the format procedure.
>
> I end up with (for example)
>
> PROC FORMAT;
> VALUE ICD7F
> 4201,4300-4309 = "1" 5120-5129 = "2" OTHER = "0" ;
>
> Now this is fine and a run of the macro (XPANDF) appears
> later but I also
> want to set
> up the corresponding as (again, for example)
>
> PROC FORMAT;
> VALUE ICD7C
> 1 = "4201,4300-4309" 2 = "5120-5129" ;
>
> Referring to the macro below I (naively as it turned out)
> thought that it
> would be
> sufficient to replace line 33 (
> %qsubstr(&temp,1,%eval(&f-1))%str( = )&n ) with
> the line
> &n%str( = )%qsubstr(&temp,1,%eval(&f-1))
> but this gave me errors and I have not been able to get the
> correct syntax
> to give
> me what I need.
>
> Could anyone let me know where I am going wrong here - I
> guess it's related
> to
> appropriate quoting but I just can't get it right.
>
> Help ?
>
> <SNIPPED LOG STUFF...>
> NOTE: AUTOEXEC processing completed.
>
> 1 %*
> --------------------------------------------------------------
> -----;
> 2 %* macro XPANDF expands a string of type
> 4201+4300-4309/5120-
> 5129 into;
> 3 %* a form contributing to a PROC FORMAT statement
> ;
> 4 %* %XPAND(4201+4300-4309/5120-5129) ->>
> ;
> 5 %* 4201,4300-4309 = "1" 5120-5129 = "2"
> ;
> 6 %*
> --------------------------------------------------------------
> -----;
> 7 %* Use as:-
> ;
> 8 %* PROC FORMAT;
> ;
> 9 %* VALUE ICD7F
> ;
> 10 %* %XPANDF(4201+4300-4309/5120-5129) OTHER="0" ;
> ;
> 11 %* To produce:-
> ;
> 12 %* PROC FORMAT;
> ;
> 13 %* VALUE ICD7f
> ;
> 14 %* 4201,4300-4309 = "1" 5120-5129 = "2" OTHER = "0" ;
> ;
> 15 %*
> --------------------------------------------------------------
> -----;
> 16 %macro XPANDF(string);
> 17 %local n temp word;
> 18 %* put a dummy last word;
> 19 %let temp = %qleft(&string%str(/?));
> 20 %let n = %index(&temp,+);
> 21 %* replace all occurencs of a plus sign (+) with a
> comma (,);
> 22 %do %while (&n > 0);
> 23 %let word = %qsubstr(&temp,1,%eval(&n-1))%str(,)
> %qsubstr(&temp,%eval(&n+1));
> 24 %let temp = %qleft(&word);
> 25 %let n = %index(&temp,+);
> 26 %end;
> 27 %* split the individual groups and generate
> appropriate line;
> 28 %let n = 1;
> 29 %let f = %index(&temp,/);
> 30 %do %while (&f > 0);
> 31 %if (&f > 1) %then
> 32 %do;
> 33 %qsubstr(&temp,1,%eval(&f-1))%str( = )&n
> 34 %end;
> 35 %let n = %eval(&n + 1);
> 36 %let temp = %qleft(%qsubstr(&temp,%eval(&f+1)));
> 37 %let f = %index(&temp,/);
> 38 %end;
> 39 %mend;
> 40 %* ;
> 41 %* run macro as a test ------->>;
> 42 %* ;
> 43 %let icd7 = 4201+4300-4309/5120-5129;
> 44 proc format page;
> 45 value icd7f
> 46 %xpandf(&icd7) other = "0" ;
> NOTE: Format ICD7F has been output.
> NOTE: The PROCEDURE FORMAT printed page 1.
> NOTE: PROCEDURE FORMAT used:
> real time 0.04 seconds
> cpu time 0.04 seconds
>
>
> NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
> NOTE: The SAS System used:
> real time 0.18 seconds
> cpu time 0.13 seconds
>
> <SAS OUTPUT FILE...>
>
> ‚ FORMAT NAME: ICD7F LENGTH: 1 NUMBER OF
> VALUES: 4
> ‚
> ‚ MIN LENGTH: 1 MAX LENGTH: 40 DEFAULT LENGTH
> 1 FUZZ: STD
> ‚
>
> ‚START ‚END ‚LABEL (VER. 8.2
> 02DEC2002:15:46:59)‚
>
> ‚ 4201‚ 4201‚1
> ‚
> ‚ 4300‚ 4309‚1
> ‚
> ‚ 5120‚ 5129‚2
> ‚
> ‚**OTHER** ‚**OTHER** ‚0
> ‚
>
|