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 (January 2006, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sun, 29 Jan 2006 20:53:05 +0000
Reply-To:     iw1junk@COMCAST.NET
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Ian Whitlock <iw1junk@COMCAST.NET>
Subject:      How to generalize a series of numerical comparisons in macro
Comments: cc: Anthony Pitruzzello <tonypit45@YAHOO.CA>

Howard kindly pointed out that a previous message of mine with the same subject line was mangled somewhere. I do not know what happened, here is a repost of the message.


You have gotten the advice asked for

%if &i > 1 %then ...

from Toby Toby Toby Toby Toby Toby Toby Toby Toby Toby ( just helping Toby keep up with David in the new year :)

And you have gotten a clue from Howard to move the PROC APPEND out of the loop.

And David went one better with the FILEVAR option.

But nobody has pointed out the folly of using variable names PRD&I and RM&I. This gives the worst append structure possible - diagonal data with missing off the diagonal.

I would suggest as an alternative to the FILEVAR, you could also use explicit concatenation.

filename mstr ( "first" "second ... ) ;


filename mstr "...*" ; /* wildcard specification */

Then the DATA step might be something like

data ms ; infile mstr ... ; /* if you stick with a macro loop */ dsnord = &i ; /* OR in casee of wildcard specification */ dsn = scan ( <the filename variable> , -1 , "/" ) ; /* OR in case of filevar option */ dsn = scan ( <the filevar variable> , -1 , "/" ) ; input @1 PRD $3. @5 RM $3. <more stuff> @128 UNIT 4. ; run ;

With the right variable names the PROC SORT can be moved out of the loop using a sort by DSN* UNIT and placing it after the APPEND if APPEND is used.

In short, the data structure is important and there is far more to think about than just the question asked.

Ian Whitlock ================ Date: Fri, 27 Jan 2006 13:34:53 -0500 Reply-To: Anthony Pitruzzello <tonypit45@YAHOO.CA> Sender: "SAS(r) Discussion" From: Anthony Pitruzzello <tonypit45@YAHOO.CA> Subject: How to generalize a series of numerical comparisons in macro Content-Type: text/plain; charset=iso-8859-1 Greetings everyone, Here’s a macro that reads in a series of data sets and stacks them. It works fine, given that, in this instance, it only reads four data sets. ___________________________________________________ %MACRO READ_MS; %DO I = 1 %TO 4; DATA MS&I; INFILE MSTR&I; INPUT @1 PRD&I $3. @5 RM&I $3. <more stuff> @128 UNIT&I 4. ; PROC SORT; BY UNIT; %IF &I=2 | &I=3 | &I=4 %THEN %DO; PROC APPEND BASE=MS1 DATA=MS&I; %END; %END; %MEND READ_MS; %READ_MS _________________________________________________ It would be easy to generalize the %DO loop to read any number of data sets. However, I don’t see how to generalize the %IF statement to correctly append any number of data sets. For example, one might think that instead of %IF &I=2 | &I=3 | &I=4 I could write %IF 2 <= &I <=4 However, a while back, someone on the List did a real nice job of explaining how MACRO treats these statements. So, when &I = 1, then “2 <= 1” = 0 (FALSE) and “0 <= 4” = 1 (TRUE), so the statement is evaluated as TRUE and MS1 is appended to itself. Does anyone see a better way of doing this? Anthony Pitruzzello

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