%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.
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;
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: