## Need Help to convert a code into a MACRO or a solution for the issue.

Solved
Occasional Contributor
Posts: 10

# Need Help to convert a code into a MACRO or a solution for the issue.

I am very new to the MACROs and need urgent help..!!

The code works fine for a single iteration if I replace &numobs with a number. The reason I need the code as a macro is so that I can use it recursively. if there is a way for me to insert the # of observations on a file (in place of &numobs) in the definition of the array even without using the MACROs; that will also solve my problem ...

As always I greatly appreciate the help and support extended by the community. Many many thanks in advance

MY CODE:

Data _null_;

set WORK.TRNS_FILLDATE (DROP= Source);

array INNER{*}DOS1-DOS&numobs;

set WORK.TRNS_ENDDATE (DROP= Source);

array OUTER{*} ENDDATE1-ENDDATE&numobs;

counter = 0;

i=1;

j = DIM(OUTER)-1;

k=2;

l=DIM(INNER);

do x= i to j;

do y= k to l;

Counter + (OUTER(x) > INNER(y));

Put 'Counter='counter;

end;

k=k+1;

end;

run;

Accepted Solutions
Solution
‎03-07-2013 01:58 AM
Frequent Contributor
Posts: 81

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

Hi Apprentice,

I am using the below code and i am getting the numobs to be resolved:-

===================================================

options mprint mlogic symbolgen;

%macro no(numobs);

Data _null_;

set WORK.TRNS_FILLDATE (DROP= Source);

array INNER{*}DOS1-DOS&numobs.;

set WORK.TRNS_ENDDATE (DROP= Source);

array OUTER{*} ENDDATE1-ENDDATE&numobs.;

counter = 0;

i=1;

j = DIM(OUTER)-1;

k=2;

l=DIM(INNER);

do x= i to j;

do y= k to l;

Counter + (OUTER(x) > INNER(y));

Put 'Counter='counter;

end;

k=k+1;

end;

run;

%mend;

%no(10);

============================================

Log:-

===================================================================

MLOGIC(NO):  Beginning execution.

MLOGIC(NO):  Parameter NUMOBS has value 10

MPRINT(NO):   Data _null_;

MPRINT(NO):   set WORK.TRNS_FILLDATE (DROP= Source);

ERROR: File WORK.TRNS_FILLDATE.DATA does not exist.

SYMBOLGEN:  Macro variable NUMOBS resolves to 10

MPRINT(NO):   array INNER{*}DOS1-DOS10;

MPRINT(NO):   set WORK.TRNS_ENDDATE (DROP= Source);

ERROR: File WORK.TRNS_ENDDATE.DATA does not exist.

SYMBOLGEN:  Macro variable NUMOBS resolves to 10

MPRINT(NO):   array OUTER{*} ENDDATE1-ENDDATE10;

MPRINT(NO):   counter = 0;

MPRINT(NO):   i=1;

MPRINT(NO):   j = DIM(OUTER)-1;

MPRINT(NO):   k=2;

MPRINT(NO):   l=DIM(INNER);

MPRINT(NO):   do x= i to j;

============================================================================

/Daman

All Replies
Solution
‎03-07-2013 01:58 AM
Frequent Contributor
Posts: 81

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

Hi Apprentice,

I am using the below code and i am getting the numobs to be resolved:-

===================================================

options mprint mlogic symbolgen;

%macro no(numobs);

Data _null_;

set WORK.TRNS_FILLDATE (DROP= Source);

array INNER{*}DOS1-DOS&numobs.;

set WORK.TRNS_ENDDATE (DROP= Source);

array OUTER{*} ENDDATE1-ENDDATE&numobs.;

counter = 0;

i=1;

j = DIM(OUTER)-1;

k=2;

l=DIM(INNER);

do x= i to j;

do y= k to l;

Counter + (OUTER(x) > INNER(y));

Put 'Counter='counter;

end;

k=k+1;

end;

run;

%mend;

%no(10);

============================================

Log:-

===================================================================

MLOGIC(NO):  Beginning execution.

MLOGIC(NO):  Parameter NUMOBS has value 10

MPRINT(NO):   Data _null_;

MPRINT(NO):   set WORK.TRNS_FILLDATE (DROP= Source);

ERROR: File WORK.TRNS_FILLDATE.DATA does not exist.

SYMBOLGEN:  Macro variable NUMOBS resolves to 10

MPRINT(NO):   array INNER{*}DOS1-DOS10;

MPRINT(NO):   set WORK.TRNS_ENDDATE (DROP= Source);

ERROR: File WORK.TRNS_ENDDATE.DATA does not exist.

SYMBOLGEN:  Macro variable NUMOBS resolves to 10

MPRINT(NO):   array OUTER{*} ENDDATE1-ENDDATE10;

MPRINT(NO):   counter = 0;

MPRINT(NO):   i=1;

MPRINT(NO):   j = DIM(OUTER)-1;

MPRINT(NO):   k=2;

MPRINT(NO):   l=DIM(INNER);

MPRINT(NO):   do x= i to j;

============================================================================

/Daman

Occasional Contributor
Posts: 10

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

Thanks Daman Bhaji... The Solution worked.

Super User
Posts: 6,785

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

I'll take your word for it, that the code works just right if you hard-code a number.  In that case, you are looking to find the number of variables within a single observation (not the number of observations).  You can work around that as long as the variable names cooperate.  More specifically, you will need the variable names that begin with DOS to include exactly the variables that belong in one array, and all variable names that begin with ENDDATE to belong in the other array.  Under those conditions, this would be all you need to change:

array inner {*} DOS:;

array outer {*} ENDDATE:;

The colon means take all variable names that begin with these characters.

Good luck.

Occasional Contributor
Posts: 10

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

Thank you Astounding; This also proved to be an amazing solution.

SAS Employee
Posts: 8

## Re: Need Help to convert a code into a MACRO or a solution for the issue.

If you really want the observation count in a macro variable, here is an easy way to create it:

%let dsid=%sysfunc(open(TRNS_FILLDATE));

%let numobs=%sysfunc(attrn(&dsid,nlobs));

%let rc=%sysfunc(close(&dsid));

🔒 This topic is solved and locked.