BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data dsn2;
set sashelp.class;
n=1;
do while (n<5);
 n+1;	
end;output;
run;

i want below output using do while and do until loops

 

Name Sex Age Height Weight
AlfredM1469.0112.5
AliceF1356.584.0
BarbaraF1365.398.0
CarolF1462.8102.5
HenryM1463.5102.5
2 REPLIES 2
Tom
Super User Tom
Super User

The code you posted is just going to create a new variable named N with the same value on every observation, so the output will look nothing like what you say you want.

 

Can you describe in words what you want as the output?

 

Looks like you just want to use one of these data steps instead, none of which need DO WHILE or DO UNTIL.

data want;
  set sashelp.class;
  if _n_<= 5;
run;

data want;
  set sashelp.class;
  if _n_>5 then stop;
run;

data want;
  set sashelp.class (obs=5);
run;

 

PaigeMiller
Diamond | Level 26

Generally, DO WHILE is not for selecting rows, or iterating over rows.

 

data dsn2;
set sashelp.class(obs=5);
run;

or

 

data dsn2;
     set sashelp.class;
     if _n_<=5;
run;

 

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 678 views
  • 0 likes
  • 3 in conversation