BookmarkSubscribeRSS Feed
mar0000
Obsidian | Level 7
%MACRO GetData;
DATA want;
	SET have;
	DO x = 1 to 12;
	DO y = 12 to 23;
	IF AGE = &x THEN AGE2 = &y;
%END; %END;
RUN;
%MEND GetData;
%GetData;

This macro produces a dataset, but the dataset has 0 observations. The error is from AGE2 = &y, but I'm not sure how to fix it.

3 REPLIES 3
ballardw
Super User

Where did you specify the values of the macro variables &x and &y?

You macro as written shouldn't even compile. This what I get compiling your macro:

56   %MACRO GetData;
57   DATA want;
58      SET have;
59      DO x = 1 to 12;
60      DO y = 12 to 23;
61      IF AGE = &x THEN AGE2 = &y;
62   %END; %END;
ERROR: There is no matching %DO statement for the %END. This
       statement will be ignored.
ERROR: There is no matching %DO statement for the %END. This
       statement will be ignored.
63   RUN;
64   %MEND GetData;

So without a successfully compiled macro there is nothing to run.

 

You do not use %end with data step Do. So that would be the first fix.

Second if you want to create values in do loops you likely want to OUTPUT the values. Otherwise only the LAST value will be in the data, nothing related to the "loops"

Third to use the value of a loop counter like X and Y do not attempt to use a macro variable.

And lastly, unless you show a small sample of the data in have and what you expect to see in the output I don't think this "macro" is needed at all.

 

%MACRO GetData;
DATA want;
   SET have;
   DO x = 1 to 12;
      DO y = 12 to 23;
	IF AGE = x THEN AGE2 = y;
        OUTPUT;
      END; 
   END;
RUN;
%MEND GetData;
%GetData;

 

Or maybe you mean something more like this??? I can't really tell what you are attempting to accomplish. It would help to provide a small example of the Have data and what you expect the output to look like.

%MACRO GetData;
DATA want;
   SET have;
   %DO x = 1 %to 12;
      %DO y = 12 %to 23;
	IF AGE = &x THEN AGE2 = &y;
      %END; 
   %END;
RUN;
%MEND GetData;
%GetData;​

 

SASKiwi
PROC Star

You need to explain what you are trying to do in your program as the way you have written it doesn't make any sense.

miriam93
Fluorite | Level 6

Yes, you should definitely explain the idea behind your code. The way the logic is written (assuming &x and &y refer to the loop variables), if Age ist between 1 and 12, Age2 is simply set to 23. You don't need a macro for that.


And also, where do you assign a value to AGE? Maybe you want to give it as an argument to the macro? This is done as follows:

 

%MACRO GetData (Age_Value);
/*Insert your macro and use &Age_Value when refering to the value of AGE*/
%Mend;

 

🙂 Miriam

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 562 views
  • 0 likes
  • 4 in conversation