DATA Step, Macro, Functions and more

simple macro question

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

simple macro question

Hi

I am trying to use a simple macro and it does not appear to be doing what i am intending it to do.  I would like to write a macro to sort 4 data sets by the same variable, called num.  The data set names differ by a numeric value.  For example the first data set is called data1, the second data2, the third data3, and the fourth data4.  All four data sets are in my work library.  Below is the code I tried to use.  Any input you have would be very helpful.  I know I can use 4 proc sorts to get this done but was attempting to do this through a macro.

%macro srt;

    %do i=1 %to 4;

    %end;

        proc sort

        data=data&;

        by num;

        run;

%mend srt;


Accepted Solutions
Solution
‎11-28-2012 07:07 PM
PROC Star
Posts: 7,365

Re: simple macro question

You are ending your loop too quickly and didn't specify the macro variable.  Try (untested):

%macro srt;

    %do i=1 %to 4;

        proc sort

          data=data&i;

          by num;

        run;

    %end;

%mend srt;

%srt

View solution in original post


All Replies
Solution
‎11-28-2012 07:07 PM
PROC Star
Posts: 7,365

Re: simple macro question

You are ending your loop too quickly and didn't specify the macro variable.  Try (untested):

%macro srt;

    %do i=1 %to 4;

        proc sort

          data=data&i;

          by num;

        run;

    %end;

%mend srt;

%srt

Contributor
Posts: 33

Re: simple macro question

Well, I tried this but now I get an error message of:

File WORK.DATA5.DATA does not exist.

I consider this an improvement as nothing was posting in my log before.

I am new to macros and am curious why you included the %srt at the end.

Super Contributor
Posts: 1,636

Re: simple macro question

/* create sample datasets */

data data1 data2 data3 data4;

input num @@;

cards;

2 4 6 4 1 38 9

;

/* define your macro */

%macro srt;

    %do i=1 %to 4;

        proc sort

          data=data&i;

          by num;

        run;

    %end;

%mend srt;

/* using the macro created */

%srt

Contributor
Posts: 33

Re: simple macro question

Oh shoot, thank you both Arthur and Linlin - the code worked - it was <again> human error on my part

Super User
Super User
Posts: 6,502

Re: simple macro question

The statements from %MACRO to %MEND just define the macro. To actually run it you need call it.  That is why Art included the %SRT at the end of the program.

If you are seeing a message about DATA5 after running the macro as defined then it looks like you are trying to reference the macro variable i after the macro has finished.  You might want to add a %LOCAL statement to your macro definition so that looping over I does not modify an existing macro variable in an outer scope.

%macro srt;

  %local i ;

  %do i=1 %to 4;

    proc sort data=data&i;

      by num;

    run;

  %end;

%mend srt;

%srt

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 244 views
  • 6 likes
  • 4 in conversation