Date: Wed, 14 May 2003 22:49:49 GMT
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Roger Lustig <trovato@BELLATLANTIC.NET>
Subject: Re: Simple macro
Content-Type: text/plain; charset=us-ascii; format=flowed
Jack Hamilton wrote:
> Is there more to this problem than you're telling us? There's no need
> to write a macro to generate 5 data set names if you already know the 5
Worse yet, it's generating *25* names! Perhaps passing the five
combinations of lib-number and year via macro params is the way to go
after all. (You don't know that you'll always have that pattern of
libnames and member names, do you?) So you need a macro to create one
data set and then append it to a master, and then call it 5 times. Make
sure to delete the master with PROC DATASETS before you start.
Good catch of the data-type thing, Jack. That one zipped right by me.
Roger (a proofreader who needs one himself)
> A few problems stand out:
> - You define NDIR and YEAR in the %MACRO line, but you don't need to.
> You don't use the values that might be passed in; you immediately
> overwrite them with values calculated in the macro.
> - Each iteration creates a data set named dir6.temp000. The second
> iteration will overwrite the data set created by the first iteration,
> the third will overwrite the second, and so on. You need to (1) create
> separate data sets and append them, or (b) read all the input data sets
> in 1 step, not 5.
> - You're creating an invalid data set name with "ir&ndir.nhds&year".
> There needs to be a dot between the library name and the member name:
> "dir&ndir..nhds&year". The single dot you have there now disappears
> because the macro facility uses it to indicate the end of the macro
> variable name.
> - You have an implicit data type conversion in "hosp_id_tmp =
> n||'_'||&year ;". Many people think it's best to avoid those.
> Since you're trying to learn, I won't give you an answer, but I'll give
> you a suggestion: Create the code you need in a text editor and make
> sure it works. Then convert it to a macro.
> Manager, Technical Development
> Metrics Department, First Health
> West Sacramento, California USA
>>>>David <david5705@HOTMAIL.COM> 05/14/2003 2:58 PM >>>
> Starting to teach myself macro language and need some help.
> Problem: Concatenate data from 5 data files:
> and create new variables: "year", "hosp_id"
> My code:
> %macro pulldata(ndir, year) ;
> %do ndir=1 %to 5 ;
> %do year=1997 %to 2001 ;
> data dir6.temp000 ;
> set dir&ndir.nhds&year (obs=5) ;
> length year $4 n $6 ;
> year='&year' ;
> n=(_n_) ;
> age=input(age_tmp, 8.) ;
> if age >= 18 ;
> hosp_id_tmp = n||'_'||&year ;
> run ;
> %end ;
> %end ;
> %mend pulldata ;
> I may be close, but no cigar (yet).
> Thanks in advance for any help.