BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jcorti
Obsidian | Level 7

 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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

10 REPLIES 10
Kurt_Bremser
Super User

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.

art297
Opal | Level 21

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

 

 

 

Jcorti
Obsidian | Level 7

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
art297
Opal | Level 21

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

Jcorti
Obsidian | Level 7

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.  

 

Kurt_Bremser
Super User

@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.

Reeza
Super User

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. 

 

Jcorti
Obsidian | Level 7

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.

art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

art297
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3793 views
  • 0 likes
  • 4 in conversation