LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (June 2002, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 13 Jun 2002 14:55:06 -0400
Reply-To:     "Fehd, Ronald J." <rjf2@CDC.GOV>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Fehd, Ronald J." <rjf2@CDC.GOV>
Subject:      Re: Macro %DO Loop
Comments: cc: Anthony Pitruzzello <statman45@HOTMAIL.COM>
Content-Type: text/plain

> From: Anthony Pitruzzello [mailto:statman45@HOTMAIL.COM] > My problem is one of passing to one macro > a set of global variables created in another macro.

Read that sentence again.

> Here's a macro that simply reads a file, where &A through &F > represent a range of years, e.g., 1997-2002:

> I want to initialize these suffix variables in a menu at the > beginning of the program. > I can do that with a set of simple %LET statements ( > %LET A=1997; %LET B=A+1; %LET C=B+1; etc).

what you said you wanted, is not what you did: %PUT _USER_; A 1997 B A+1 C B+1 to get the sequential list you expect, 1997, 1998, 1999 use this: %LET B = %eval(&A. + 1); %LET C = %eval(&A. + 2);

> Here's what puzzles me: If I try this-- > > %MACRO SETYRS; > DATA MAC1; > RETAIN A B C D E F 0; > %GLOBAL A B C D E F; > ARRAY ALPHA{*} A B C D E F; > %DO I=1 %TO 6; > ALPHA{&I} = &YR + &I; > %END; > %MEND SETYRS;

I missed something, or you left something out: in which case you should get an error.

Where is mac-var YR declared? I'm thinking that YR is &A., correct?

your %DO loop generates the following code: Alpha(1) = &YR + 1; Alpha(2) = &YR + 2; *...; Alpha(6) = &YR + 6;

so data set MAC1 contains variables A:F with values whatever &YR is + 1:6; those are not macro variables, nor global variables.

> %MACRO READ28; > DATA LN28; INFILE IN1; INPUT > @2 SID $8. @128 STATUS $1. @130 LNNO 2. @133 UNIT 4. @; > IF LNNO=28 THEN INPUT > @50 RGE&A 3. @46 RGE&B 3. @42 RGE&C 3. > @38 RGE&D 3. @34 RGE&E 3. @30 RGE&F 3. > @79 RST&A 1. @78 RST&B 1. @77 RST&C 1. > @76 RST&D 1. @75 RST&E 1. @74 RST&F 1. > @101 MGE&A 3. @97 MGE&B 3. @93 MGE&C 3. > ; > ELSE DELETE; > DROP STATUS LNNO; > %MEND READ28; > %READ28 > > > -- and if I follow %SETYRS with a PROC PRINT; VAR A B C, the > PROC prints the > expected values, which tells me > those variables exist outside the macro.

Knot! as macro variables.

> However, these appear to be ordinary SAS variables (A B C) rather than > global macro variables (&A &B &C). If I call the macros sequentially, > %SETYRS %READ28, %READ28 does not recognize the macro > variables I supposedly > created in %SETYRS. As far as %READ28 is concerned, RGE&A is > just RGE. > Does anyone know why?

commentary: your macros have no complexity: no %IF nor %DO so they could be plain data steps in a program or, if you want to repeat that same code they can be in a separate files, which you then %include

Yes, SETYRS does contain a %DO loop, but that is appropriately a data step do loop.

From what you describe you have one parameter: start year Everything else can be generated from that one macro variable.

%LET Y1 = 1997;%*SET START YEAR;

%LET Y2 = %eval(&Y1. +1); %*...; %LET Y6 = %eval(&Y1. +5);

DATA LN28; DROP STATUS LNNO; INFILE IN1;%*check options you may be able to read exactly line#28; INPUT @130 LNNO 2. @; IF LNNO=28 THEN DO; INPUT @2 SID $8. @128 STATUS $1. @133 UNIT 4.

@50 RGE&YR1. 3. @46 RGE&YR2. 3. @42 RGE&YR3. 3. @38 RGE&YR4. 3. @34 RGE&YR5. 3. @30 RGE&YR6. 3. STOP;END;

Ron Fehd the macro maven CDC Atlanta GA USA RJF2@cdc.gov

--> cheerful provider of UNTESTED SAS code from the Clue?Gee!Wrx <--

By using your intelligence you can sometimes make your problems twice as complicated. -- Ashleigh Brilliant

remember perspective: the error is not always where it seems to occur! -- RJF2


Back to: Top of message | Previous page | Main SAS-L page