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

I am trying to develop a do loop for nonconsecutive number like:

1 to 45, plus 101, 105

How to writhe the code?

I tried %do i=1 % to 45, 101, 105, but doesn't work. Any suggestion? Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
Opal | Level 21

Just create a separate loop variable:

%do i=1 %to 47;
  %if &i <= 45 %then %let j = &i;
  %else %if &i = 46 %then %let j = 101;
  %else %if &i = 47 %then %let j = 105;
    %put j = &j;
  %* more statements;
%end;

View solution in original post

3 REPLIES 3
SASKiwi
Opal | Level 21

Just create a separate loop variable:

%do i=1 %to 47;
  %if &i <= 45 %then %let j = &i;
  %else %if &i = 46 %then %let j = 101;
  %else %if &i = 47 %then %let j = 105;
    %put j = &j;
  %* more statements;
%end;
Tom
Super User Tom
Super User

@Jerrynetwork wrote:

I am trying to develop a do loop for nonconsecutive number like:

1 to 45, plus 101, 105

How to writhe the code?

I tried %do i=1 % to 45, 101, 105, but doesn't work. Any suggestion? Thank you!

 


That is easy to do with a DO loop.

do i=1 to 45,101,105 ;

It is harder with a macro %DO loop as the macro %DO statement is not as powerful as the data step DO loop.

ballardw
Super User

@Jerrynetwork wrote:

I am trying to develop a do loop for nonconsecutive number like:

1 to 45, plus 101, 105

How to writhe the code?

I tried %do i=1 % to 45, 101, 105, but doesn't work. Any suggestion? Thank you!

 


"Doesn't work" is such a vague description as to be meaningless.

I suspect that you get an ERROR message similar to

ERROR: A character operand was found in the %EVAL function or %IF
       condition where a numeric operand is required.

If so you should include complete code, entire data step, procedure and/or macro definition and the error messages from the log. Copy and paste the text into a text box opened on the forum with the </> icon above the message window. Then possible describe what the result should be as it is impossible to tell from your incomplete snippet

.

Note that your code is incomplete for the macro language as it should be %to not 'to' with the %do.

The comma is not part of a numeric value so that throws the character operand as %to expects something that resolves to an integer.

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 266 views
  • 4 likes
  • 4 in conversation