BookmarkSubscribeRSS Feed
supersonic
Calcite | Level 5


Can anyone please give an example where a do loop can be used but %do loop cannot be used

6 REPLIES 6
kuridisanjeev
Quartz | Level 8

Hi..

%Do can be used only with in the Macro,but in case of Do,you can use any where of the program even with in the macro.

which means you can not use %do in open code..

Data Test;

%do i=1 %to 10;

output;

%end;

Run;

Above code gives you error,so in above scenario you need to use Do.

Hope this helps..

Regards.

Sanjeev.K

kuridisanjeev
Quartz | Level 8

Edited my post and given small example too..

Hope that is what you looking for..

Regards.

Sanjeev.K

Ksharp
Super User

You can do it:

do i=1 to 10 by 0.1 ;

But you can not do it:

%do i=1 %to 10 by 0.1;

supersonic
Calcite | Level 5

thank u

do i=1,2,3;   will work

%do i=1,2,3;  will give an error

kuridisanjeev
Quartz | Level 8

Ohhh too good to know that..

But why we can't use By in %do ??

is there any reason ???

Regards.

Sanjeev.K

Astounding
PROC Star

You can use %BY, but you can't use a decimal fraction like 0.1.

The last construction where DO can be used but %DO cannot is a combination of a range with WHILE or UNTIL.

Legitimate:     do i=1 to 10 until (age > 21);

No good:          %do i=1 %to 10 %until (&age > 21);

You can use %DO %WHILE or %DO %UNTIL, you just can't combine that with a range of values.

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
  • 6 replies
  • 818 views
  • 2 likes
  • 4 in conversation