do loop n times

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

do loop n times

 Hi Team,

 

I want to do a "do loops" 3 times with 75 obervations

With my example I am getting from 00 to 24 with 25 observations 

 

Do you know how to do it?

 

Thanks

 

data test;
%do i1=0  %to 24 ;
%let i = %sysfunc(putn(&i1,z2.));
%end;

 


Accepted Solutions
Solution
‎02-25-2017 10:45 PM
PROC Star
Posts: 7,477

Re: do loop n times

I just copied and hadn't tested your code. Try the following:

 

data test (keep=i);
  do ii=1 to 100;
    do jj=1 to 25;
      i = put(jj,z2.);
      output;
    end;	
  end;
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Super User
Posts: 7,809

Re: do loop n times

You're doing nothing besides creating (local) macro variable i1 with value 25 and (local) macro variable i with value 24.

Since this is obviously only part of your code, as it would not work on its own (%macro and %mend missing), please post your whole code, the log and the expected result.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 7,477

Re: do loop n times

[ Edited ]

What are you trying to do? Your example doesn't produce any records and won't run as it incorporates macro code outside of a macro.   If you're trying to create records, you'll have to create and set the value of a variable rather than an a macro variable.

 

You can always nest loops. e.g.

data test;
  do ii=1 to 3;
    do jj=0  to 24 ;
       i = %sysfunc(putn(jj,z2.));
      output;
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

 

 

Contributor
Posts: 42

Re: do loop n times

this is the output that I need:

 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
PROC Star
Posts: 7,477

Re: do loop n times

[ Edited ]

Then just use something like:

 

data test (keep=i);;
  do ii=1 to 3;
    do jj=1  to 25 ;
       i = putn(jj,z2.);
      output;
    end;
  end;
run;

Art, CEO, AnalystFinder.com

Contributor
Posts: 42

Re: do loop n times

[ Edited ]

This worked properly:

 

data test (keep=jj);;
  do ii=1 to 100;
    do jj=1  to 25 ;
      output;
    end;
	
  end;
run;

Why this does not work??

 

data test (keep=jj);;
  do ii=1 to 100;
    do jj=1  to 25 ;
      i = %sysfunc(putn(jj,z2.));
      output;
    end;
	
  end;
run;
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC 
       or %QSYSFUNC function reference is terminated.
43                i = %sysfunc(putn(jj,z2.));
                                            _
                                            22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, INPUT, PUT.  

 

Super User
Posts: 7,809

Re: do loop n times


Jcorti wrote:

Why this does not work??

 

data test (keep=jj);;
  do ii=1 to 100;
    do jj=1  to 25 ;
      i = %sysfunc(putn(jj,z2.));
      output;
    end;
	
  end;
run;
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC 
       or %QSYSFUNC function reference is terminated.
43                i = %sysfunc(putn(jj,z2.));
                                            _
                                            22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, INPUT, PUT.  

 


%sysfunc is a macro function that is resolved by the macro processor before the data step is compiled, so it never has access to data step variable values, only names. The way it's written here, it then tries to run the data step function as

putn("jj",z2.)

Since putn expects a numeric value as its first argument -> boom.

The %sysfunc wrapper was a remnant from your earlier code and should have been removed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 19,822

Re: do loop n times

You have one loop that creates 0 to 24 - you've explicitly put that in your code, if you want 1 to 25 change the values in your %DO/%TO statement.

If you want it repeated 3 more times, wrap it another loop that goes through three times. 

 

It doesn't make sense with macro variables though becaue the values will get overwritten.  

Art's code shows the structure you need whether you're using macro's or a data step.

 

You also don't specify what output type you need, a dataset, log output or a displayed report ie in HTML or PDF. 

You don't need the %SYSFUNC in Art's code so I removed that. 

 

Contributor
Posts: 42

Re: do loop n times

3 times is only an example that I need, actually I need 100 times repetead the same loop in order to create the table required with one column like shown above.

I need that table because I will concatenate with another table.

PROC Star
Posts: 7,477

Re: do loop n times

So, can we presume that you now have what you needed?

 

Art, CEO, AnalystFinder.com

 

Solution
‎02-25-2017 10:45 PM
PROC Star
Posts: 7,477

Re: do loop n times

I just copied and hadn't tested your code. Try the following:

 

data test (keep=i);
  do ii=1 to 100;
    do jj=1 to 25;
      i = put(jj,z2.);
      output;
    end;	
  end;
run;

Art, CEO, AnalystFinder.com

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 334 views
  • 0 likes
  • 4 in conversation