Use macro variables created outside a data step in a subsequent data step

Reply
New Contributor
Posts: 4

Use macro variables created outside a data step in a subsequent data step

[ Edited ]

Hello, everyone!

 

For a project, we have to assess the mean exposure of animals (mice) to PM25. We have been given two data sets, air (with variables DATE (each day from Jan 1 2008 to Dec 31 2011 in date format), PM25 (PM25 levels for each day), and I created variable Average, for the mean exposure to PM25 on a specific day, e.g., the average exposure to PM25 on the second line of obs is PM25 on day 1 plus PM25 on day 2 divided by 2, etc.). An example of the data is given below:

 

Obs date PM25 average

1 01JAN2008 10.9167 10.9167

2 02JAN2008 10.9167 10.9167

3 03JAN2008 17.6250 13.1528

4 04JAN2008 31.0833 17.6354

5 05JAN2008 5.0833 15.1250

6 06JAN2008 4.1667 13.2986

7 07JAN2008 2.8333 11.8036

8 08JAN2008 2.8750 10.6875

9 09JAN2008 13.5833 11.0093

10 10JAN2008 14.7500 11.3833

11 11JAN2008 . 11.3833

12 12JAN2008 . 11.3833

13 13JAN2008 . 11.3833

14 14JAN2008 . 11.3833

15 15JAN2008 . 11.3833

16 16JAN2008 . 11.3833

17 17JAN2008 . 11.3833

18 18JAN2008 . 11.3833

19 19JAN2008 . 11.3833

20 20JAN2008 . 11.3833

21 21JAN2008 . 11.3833

22 22JAN2008 12.7000 11.5030

23 23JAN2008 0.8182 10.6126

 

We also have data set ANIMAL, which includes the birthdate and date of assessment of a biomarker. An example is given below:

birth_

Obs   ID      date              pft_date       biomarker

1      38886 01APR2008 07OCT2011 279.36

2      38949 08APR2008 20JUL2011 304.67

3      38957 09APR2008 19AUG2011 280.78

4      38958 09APR2008 01SEP2011 259.08

5      38973 10APR2008 19SEP2011 229.85

6      38981 11APR2008 15AUG2011 355.20

7      38989 12APR2008 12AUG2011 274.00

8      38993 12APR2008 14OCT2011 353.24

9      38994 12APR2008 11OCT2011 262.81

10    39000 13APR2008 25JUL2011 240.00

 

We need to assess the mean exposure to PM25 (from birth to date of  biomarker assessment) for each mouse. I tried to go about connecting the two data sets by creating macro variables for date and Average PM25 from data set AIR, but I'm having trouble then using these macro variables (in an array I was thinking) in data set ANIMAL. I have never used code like this before so I'm clearly misunderstanding how macro variables created outside a data step can be used. Here is what I tried:

 

/*Create macro variables for Measurement Date and Average PM25*/

data _null_;

   set air;

    suffix=put(_n_, 10.);

    call symput(cats('ExpDt',suffix), (put(date, mmddyy10.)));

    call symput(cats('AvPM25', suffix), average);

run;

 

%put &ExpDt1 &AvPM251;

 

data test;

   set animal;

 

   &array ExpDt(*) &ExpDt.: ;

    &array AvPM25(*) &AvPM25.: ;

 

   &do i=1 to &Dim(&ExpDt.);

       &if &ExpDt. EQ birth_date &then Exp1=&AvPM25.;

       &if &ExpDt. EQ pft_date &then Exp2=&AvPM25.;

   &end;

 

   AvgPM25Exp=(Exp2-Exp1);

run;

 

Any help would be greatly appreciated.

Thank you!

SAS Super FREQ
Posts: 497

Re: Use macro variables created outside a data step in a subsequent data step

Posted in reply to SAS_help_needed

Get rid of the ampersands in &array, &then, &do, &dim, and &if.  Just use the ampersands in conjunction with the macro variable names that you created. I did not try to understand your logic, but getting rid of the superfluous ampersands will get you one step closer.

 

While you can use symput, symputx is easier and requires less programming.  You should look it up.

 

 

New Contributor
Posts: 4

Re: Use macro variables created outside a data step in a subsequent data step

Posted in reply to WarrenKuhfeld

Thank you very much, I'll try that!

Super User
Posts: 6,642

Re: Use macro variables created outside a data step in a subsequent data step

Posted in reply to SAS_help_needed

I think you would need to scrap the macro language entirely, and use arrays.  For example, create one array holding all the PM25 values, and one holding all the AVERAGE values.  I put an upper bound of December 31, 2008, but that is easily adjustable if it needs to be changed::

 

data want;

array PM25_ {'01jan2008'd:'31dec2008'd} _temporary_;

array avg_ {'1jan2008'd:'31dec2008'd} _temporary_;

if _n_=1 then do until (done);

   set PM25_levels end=done;

   PM25_{date} = PM25;

   avg_ {date} = average;

   drop PM25 average;

end;

set ANIMAL;

.....

 

This does assume that all your dates are actually SAS dates, and not character strings.

 

Assuming I'm at least in the ballpark here, now it just takes using the information from ANIMAL to find and process the proper array elements.  I wasn't sure if you would actually need just one of the arrays and not both, but this could be a viable starting point.

New Contributor
Posts: 4

Re: Use macro variables created outside a data step in a subsequent data step

Posted in reply to Astounding

Thank you!

 

I'm afraid I can't get this to work though. I get the following error although the date variables are numeric formatted as date9.:

 

3163 data want;

3164

3165 array PM25_ {'01JAN2008'd:'31DEC2008'd} _temporary_;

------------

22

200

3166 array avg_ {'01JAN2008'd:'31DEC2008'd} _temporary_;

------------

22

200

ERROR 22-322: Syntax error, expecting one of the following: an integer constant, *.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

Thank you again for the help!

Super User
Posts: 6,642

Re: Use macro variables created outside a data step in a subsequent data step

[ Edited ]
Posted in reply to SAS_help_needed

I can't test any of this until Monday ...

 

I thought date literals could serve as the bounds of the array.  But if that's wrong, you can work around it:

 

data _null_;

call symputx("arr_begin', '01jan2008'd) ;

call symputx('arr_end', '31dec2008'd) ;

run;

 

Then:

 

data want;

array PM25_ {&arr_begin : &arr_end} _temporary_;

array avg_   {&arr_begin : &arr_end} _temporary_;

 

Let's see if that gets by the difficulty.  If there's some other problem, you'll find out soon enough.  Again, can't test it right now, but could there be a reason that temporary arrays have to start at element #1?

 

 

CONFIRMING, based on TESTING:

 

This version works.  By defining macro variables before the DATA step begins, they can serve as the dimension boundaries for a _TEMPORARY_ array.

New Contributor
Posts: 4

Re: Use macro variables created outside a data step in a subsequent data step

Posted in reply to Astounding

Thank you very much for your help!

Ask a Question
Discussion stats
  • 6 replies
  • 140 views
  • 0 likes
  • 3 in conversation