BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phoenix_LJ
Obsidian | Level 7
  • Thank you very much, but I'd like to ask you a question. Thank you

     Code as follws:

%macro Sets(start,end,prefix);
%do i = start %to end;
	&prefix.||&i.||' ' 
%end;
%mend; 
%Sets(2005,2050,data);
  • I want to generate a program like this:

    data2005 data2006 data2007 ......data2050;
    But my macro does not run successfully, how to modify this macro program
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

To use macro parameters in the macro, you need to address them like macro variables, and you do not need concatenation operators, as in macro everything is text:

%macro Sets(start,end,prefix);
%do i = &start. %to &end.;
	&prefix.&i 
%end;
%mend; 

You can use this like this:

data want;
set
  &sets(2005,2050,data)
; /* this semicolon is NOT part of the macro call, it terminates the SET statement! */
run;

View solution in original post

10 REPLIES 10
Phoenix_LJ
Obsidian | Level 7

 

  • I want to use this macro program for the following program

data WeekOfDate;
	set %sets(start=2005,end=2020,prefix=data);
run;
PeterClemmensen
Tourmaline | Level 20

A few corrections.

 

  1. Start, end and prefix are now macro variables. That means you should reference them with &.
  2. The macro language is a text language. Consider everything as text. This means that the concatenation operators are redundant and so is the ' ' in the end. 
  3. To test if your macro (function) returns the correct result, test it with a %Put Statement as below. 

Hope this helps 🙂

 


%macro Sets(start,end,prefix);
%do i = &start. %to &end.;
&prefix.&i.
%end;
%mend; 


%put %Sets(2005,2050,data);
Kurt_Bremser
Super User

To use macro parameters in the macro, you need to address them like macro variables, and you do not need concatenation operators, as in macro everything is text:

%macro Sets(start,end,prefix);
%do i = &start. %to &end.;
	&prefix.&i 
%end;
%mend; 

You can use this like this:

data want;
set
  &sets(2005,2050,data)
; /* this semicolon is NOT part of the macro call, it terminates the SET statement! */
run;
Phoenix_LJ
Obsidian | Level 7

First of all, thank you very much.

But,if you have two data set names 'data2005','data2006'。

now,use programs as follows:

/*define*/
%macro Sets(start,end,prefix);
%do i = &start. %to &end.;
	&prefix.&i. 
%end;
%mend; 


data a;
set
%Set(2005,2006,data)
;
run;

 

  • Unfortunately, the program will not run。

Kurt_Bremser
Super User

You define macro Sets, but you use macro set (missing s).

If something else is the issue, please post the complete log of the data step, using this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

Phoenix_LJ
Obsidian | Level 7

I'm very sorry for wasting your time, and I would like to express my heartfelt thanks for your help!

Kurt_Bremser
Super User

@Phoenix_LJ wrote:

I'm very sorry for wasting your time, and I would like to express my heartfelt thanks for your help!


Really no waste, sometimes a second set of eyes is needed. Been there, done that, have the T-shirt.

 

Might be material for another Maxim.

PeterClemmensen
Tourmaline | Level 20

Why does it not run? What does the log say?

Kurt_Bremser
Super User

Proof of concept:

data data2005;
set sashelp.class;
run;

data data2006;
set sashelp.class;
run;

%macro Sets(start,end,prefix);
%do i = &start. %to &end.;
	&prefix.&i 
%end;
%mend; 

data want;
set
%Sets(2005,2006,data)
;
run;
Phoenix_LJ
Obsidian | Level 7
thanks for your help!
This is a question that should not arise!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 850 views
  • 3 likes
  • 3 in conversation