BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jdub
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

5 REPLIES 5
art297
Opal | Level 21

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

jdub
Calcite | Level 5

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.

Linlin
Lapis Lazuli | Level 10

/* 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

jdub
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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