DATA Step, Macro, Functions and more

SAS Macro problem

Reply
Occasional Contributor
Posts: 5

SAS Macro problem

Hey All,

I've written a macro to do the following;

Step 1 is to create a date macro to help import files based on the date on the filename. The second step is to then macro the import process / sorting / merge process based on the date macro. The first step seems to run fine, but when I try and import the datasets within step 2, I get an error related to date not resolved:


%macro dates

%do i=1 %to &num_months.;

data _null_;

call symput("date_&i.",strip(put(intnx('months',&startdate.,&i.)-1,date9.)));

run;

%end;

%do i=1 %to &num_months.;
%put &&date_&i..;

%end;

%mend dates;

  %dates

libname temp 'C:\Documents and
Settings\My Documents
;

%macro merge_;

  %do i=1 %to &num_months.;


data debt_&&date_&i..;
  
set temp.bal_&&date_&i..;
          
year_month = intnx('month',&&date_&i..,'beginning');
        
format year_month DDMMYY10.;
   
run;
    
%end;

  %mend merge_;

  %merge_

Log:

19   %macro merge_;
20   %do i=1 %to &num_months.;
21       data debt_&&date_&i..;
22       set temp.bal_&&date_&i..;
23           year_month = intnx('month',&&date_&i..,'beginning');
24           format year_month DDMMYY10.;
25       run;
26
27       %end;
28   %mend merge_;
29   %merge_
WARNING: Apparent symbolic reference DATE_1 not resolved.
NOTE: Line generated by the macro variable "I".
1     debt_&date_1.
           -
           22
            -------
            201
WARNING: The Base Product product with which DATASTEP is associated will expire within 30 days. Please
         contact your SAS installation representative to have it renewed.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_,
              _LAST_, _NULL_.

ERROR 201-322: The option is not recognized and will be ignored.

NOTE: Line generated by the macro variable "I".
1     temp.bal_&date_1.
                       -
                       22
                        -------
                        201
WARNING: Apparent symbolic reference DATE_1 not resolved.
ERROR: File TEMP.bal_.DATA does not exist.
NOTE: Line generated by the macro variable "I".
1    &date_1.
     -
     386
     76
      -------
      201
WARNING: Apparent symbolic reference DATE_1 not resolved.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, ;, END, KEY,
              KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 201-322: The option is not recognized and will be ignored.

ERROR 386-185: Expecting an arithmetic expression.

ERROR 76-322: Syntax error, statement will be ignored.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.debt_ may be incomplete.  When this step was stopped there were 0
         observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


I'm new to macros, but essentially, the objective is to create a date macro depending on the number of files produced and then macro the import process x number of times without having to write 6 data steps in this case. The above log is only for one file, but there are 6 of the error above (one for each of the six files).

Thanks.

PROC Star
Posts: 7,489

Re: SAS Macro problem

There are a number of problems with your code including, but definitely not limited to:

you left the semicolon off at the end of your first line

you refer to macro variables that are never declared

your libname statement is missing a closing quote

I think you might be trying to accomplish something like:

%macro dates(startdate,num_months);

  %do i=1 %to &num_months.;

   data _null_;

     call symput("date_&i.",strip(put(intnx('months',"&startdate."d,&i.)-1,date9.)));

   run;

  %end;

%do i=1 %to &num_months.;

   %put &&date_&i..;

%end;

libname temp 'C:\Documents and Settings\My Documents';

  %macro merge_;

    %do i=1 %to &num_months.;

      data debt_&&date_&i..;

        set temp.bal_&&date_&i..;        

        year_month = intnx('month',"&&date_&i."d,0,'beginning');      

        format year_month DDMMYY10.; 

      run;   

    %end;

  %mend merge_;

  %merge_

%mend dates;

%dates(1jan2014,6)

Super User
Super User
Posts: 7,074

Re: SAS Macro problem

The most likely cause of the error message you received is that your first macro defined local macro variables that are gone by the time the second macro runs.  Hence DATE_1 macro variable does not exist.

One simple solution is to use CALL SYMPUTX() and set the optional third positional argument to 'G' to force the macro variables into the global macro table.

Occasional Contributor
Posts: 5

Re: SAS Macro problem

thanks for the replies. Tom- can you explain a little further on your solution?

thanks

Super User
Super User
Posts: 7,074

Re: SAS Macro problem

Try running this little example.

%macro localvar;

data _null_;

%do i=1 %to 2 ;

   call symput("local&i",'X');

%end;

run;

%mend localvar;

%macro globalvar;

data _null_;

%do i=1 %to 2 ;

   call symputx("global&i",'X','G');

%end;

run;

%mend globalvar;

%localvar;

%put local1=&local1 ;

%globalvar;

%put global1=&global1 ;

Valued Guide
Posts: 2,177

Re: SAS Macro problem

going a little further than @Tom and I would suggest solving the problem by generating the dates in the "merge" macro.

From what you posted, it appears all your data are already imported, so you need only one merge step.

A macro might help, but only to create the list of data sets to be merged. (I assume the all data-sets have the same structure - if you don't want side-by-side merge but a concatenation of the data on separate rows, the statement changes from merge to SET but the solution I propose doesn't change.

If they cannot sensibly be merged (or SET) in one step, perhaps you might explain why, and what you are attempting, and for simplicity, please explain without reference to macros.

peterC

PROC Star
Posts: 7,489

Re: SAS Macro problem

I agree with what and said, but what to bring up another point in the code I originally proposed.  One really should avoid nesting macros as they have to be compiled every time they are called.  I wrote it that way simply because I wanted to know if it was achieving the result you wanted and that was the quickest way (for me) to convert your code.

If that was the desired result, the following is a method for doing the same thing, but only using one macro and avoiding the problem of whether the macro variables have to be declared as being global:

%macro dates(startdate,num_months);

  %do i=1 %to &num_months.;

   data _null_;

     call symput("date_&i.",strip(put(intnx('months',"&startdate."d,&i.)-1,date9.)));

   run;

   %put &&date_&i..;

  

   data debt_&&date_&i..;  

     set temp.bal_&&date_&i..;          

     year_month = intnx('month',"&&date_&i."d,0,'beginning');        

     format year_month DDMMYY10.;   

   run;     

  %end;

%mend dates;

libname temp 'C:\Documents and Settings\My Documents';

%dates(1jan2014,6)

Occasional Contributor
Posts: 5

Re: SAS Macro problem

Arthur - thanks, i'll give this solution a go and give some feedback.

With regards to what I'm trying to acheive, this is the structure:

1. There are up to 13 historical files that require importing. (reason just the one set of files, is for the debt files, a month / year variable is required for reporting purposes, whereas the the other 13 files that are to be merged with the debt files, only three variables are required thus the temp. library is associated for the folder in my 'documents' folder. The initial code posted had a start date in July, but there were only 6 files available, hence the 'number of months's used.

The next step is to sort both sets of file by the account number and then merging them together by the account number. Finally, the outputs of these (up to 13 files) can be appended together.

Ask a Question
Discussion stats
  • 7 replies
  • 1386 views
  • 0 likes
  • 4 in conversation