BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

Any leads on macro program to repeat the value for 'n' times? Assume I have the macro variables called 'name' and 'n'. If the value is 'David' and '100' then I want the name to print 100 times in the log.

 

Desired Output: 100 times value of name should be printed.

 

David

David

.....

David

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Bizarre request. I get the feeling there's a lot more you haven't told us ... however a very simple do-loop works

 

%let name=David;
%let n=100;

%macro do_this;
     %do i=1 %to &n;
          %put &name;
     %end;
%mend;
%do_this

  

--
Paige Miller
Quentin
Super User

EDIT:

Sorry, off-by-one error below.  I hate that the developer of REPEAT() thought it should return the first argument n+1 times.  So to have David appear 100 times, you need:

%put %sysfunc(repeat(&name,&n-1)) ;

 

There is a REPEAT function, so you can do it without a macro:

 

%let name=David ;
%let n=100 ;
%put %sysfunc(repeat(&name,&n)) ;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Tom
Super User Tom
Super User

The N provided to REPEAT() is the number of EXTRA copies to create.  So subtract one.

%put %sysfunc(repeat(&name,&n-1)) ;

 

Quentin
Super User

Thanks Tom, I only remembered that while I finished my breakfast. : )

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 762 views
  • 2 likes
  • 4 in conversation