| Date: | Fri, 12 Apr 1996 09:30:27 EDT |
| Reply-To: | whitloi1@WESTATPO.WESTAT.COM |
| Sender: | "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU> |
| From: | Ian Whitlock <whitloi1@WESTATPO.WESTAT.COM> |
| Subject: | Re: Macro clarification |
|
Subject: Macro clarification
Summary: Classic macro vs DATA step array confusion is resolved, but
formats provide the solution to real problem.
Respondent: Ian Whitlock <whitloi1@westat.com>
Erik Godfrey <sg94x892@DUNX1.OCS.DREXEL.EDU> originally asked how do
you bump macro variable I in the variable IN_&i during execution of the
DATA step. This is a classic confusion about macro. The proper way to
change the reference to a variable during execution of a DATA step is
with arrays not macro code.
array in (2) $ 200 in_1 in_2 ;
retain i 1 ;
/* when appropriate */
i = 2 ;
/* reference in ( i ) */
But Erik has now clarified his problem (see his message at the end) as
opposed to telling one where he got stuck in his solution.
He has strings like 1-3,5,7-10 referring to a question ids and he
wants to produce code (in pseudo form) like:
if q24 in (1-3,5,7-10) and q32 in (7,9,11-14,17) ;
Of course the IN operator does not understand the notation, hence he
spells it out. The problem is that some strings spelled out require
more than 200 bytes.
Personally I would say the IN operator is the wrong tool. I would use
formats.
proc format ;
value q24f 1-3,5,7-10 = 'GOOD' ;
value q32f 7,9,11-14,17 = 'GOOD' ;
run ;
Now the final form for the wanted code is
if put ( q24 , q24f4. ) = 'GOOD' and
put ( q32 , q32f4. ) = 'GOOD' ;
Note that the need for most of the steps Erik spells out are no longer
needed.
Ian Whitlock <whitloi1@westat.com>
---------------------------------------------------------------------
Author: Erik Godfrey <sg94x892@DUNX1.OCS.DREXEL.EDU> at internet-e-mail
Date: 4/11/96 2:36 PM
Priority: Normal
BCC: WHITLOI1 at REC
TO: SAS-L@uga.cc.uga.edu at Internet-E-Mail
Subject: Macro clarification
------------------------------- Message Contents -------------------------------
Thanks to everyone who responded to my posting (Need macro help, ver 2).
Sorry the example I gave was rather vague; I will try to clarify what I
need here.
The situation is this: I have two strings, one called "question
string", the other called "logic string". I am parsing both of these
strings. Question string is being parsed as follows: the string will appear
in a form similar to this: 1-3,5,7-10. There is also a variable called
"qid" associated with this string. After it is parsed, I will have 8
observations, which will include the qid and question. So, for the
preceding example, let's assume qid #24 is associated with question
string. So, our dataset will appear as follows:
obs seq qid question (Seq is a sequence number. There may be
--- --- --- -------- multiple question strings for a particular
1 1 24 1 group to process, so each question string
2 1 24 2 gets its own sequence number)
3 1 24 3
4 1 24 5
5 1 24 7
6 1 24 8
7 1 24 9
8 1 24 10
What I am then doing is this: I am creating a new dataset by setting
the previously defined dataset. I am building "in" statements from the
parsed question string, and am storing this "in" statement in a dataset
variable called in_&i (where &i is an integer value, starting at 1). So,
the value of in_1 could look as follows: in(1,2,3,5,7,8,9,10). The
problem is with the 200 character limit of a variable - if the string I
am building exceeds 200 characters, I want to increment the value of &i
by 1 and store the remaining number of characters in in_2 (assuming the
number of characters to be stored here does not exceed 200 characters). I
would prefer not to write these individual in statements to external
files, first of all, to avoid using an excessive number of %include
statements later, and secondly, because I am then taking these individual
in statements and building subsetting if statements in a succeeding
dataset. This is being done as follows: I now have a dataset which may
appear as follows:
obs seq qid in_1
--- --- --- ----
1 1 24 in(1,2,3,5,7,8,9,10)
2 2 32 in(7,9,11,12,13,14,17)
The logic string may appear as: 1 & 2.
What I am doing is replacing the numbers in the logic string with the
contents of the in_1 statement, and writing this result to an external file
to be included later with a %include. The file for this example will read as
follows: if q24 in(1,2,3,5,7,8,9,10) and q32 in(7,9,11,12,13,14,17); All of
this is working fine - my problem, as stated earlier, is
generating multiple in_&i variables for a distinct seq number if the in
statement being built exceeds 200 characters (if needed, when writing out
the subsetting if statement to the file, I would append any subsequent
in_&i statements, i.e., in_2, to the in_1 in statement).
I hope this explanation clarifies what I need. Again, thanks to
everyone who helped.
--
============================================================================
== Erik Godfrey ===
== Programmer/Analyst ===
== Hay Group, Inc. ===
== Philadelphia, PA ===
============================================================================
|