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

Hi Experts,

 

I am using below mentioned code but getting repeated observation and for first observation OUT variable has missing value...WHY??

 

data new ;
do i=1 to 10;
if mod(i,2)=0 then out=i;
output;
end;
run;

 

OUTPUT:

Obs     i    out
1     1      .
2     2      2
3     3      2
4     4      4
5     5      4
6     6      6
7     7      6
8     8      8
9     9      8
10    10     10

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

Then do something like this:

data new ;
do i=1 to 10;
if mod(i,2)=0 then output;
end;
run;
proc print;run;

View solution in original post

8 REPLIES 8
LinusH
Tourmaline | Level 20
Perhaps you need an else statement?
Data never sleeps
AncaTilea
Pyrite | Level 9

Hi.

if you go through the do-loop, you will see how values get assigned to out:

for i = 1, mod(i,2) = 0 is False, so out = .;

output this record;

for i = 2, mod(i,2) = 0 is true, so out = 2;

output this record;

for i = 3, mod(i,2) = 0 is False, so out = 2;

output this record;

 

for i = 4, mod(i,2) = 0 is true, so out = 4;

output this record;

 

if you want out to take a different value for the cases when mod(i,2) is false, then assign it.

 

Something like,

if mod(i,2) = 0 then out = i;else out = .;

 

 

I hope this helps.

 

Anca.

 

 

Rahul_SAS
Quartz | Level 8
i simply want every second obs only.
AncaTilea
Pyrite | Level 9

Then do something like this:

data new ;
do i=1 to 10;
if mod(i,2)=0 then output;
end;
run;
proc print;run;

Rahul_SAS
Quartz | Level 8
hi AncaTilea,
why for i=3 var OUT has a valur 2???
AncaTilea
Pyrite | Level 9

because it carries down from the information above...

You never told SAS to change the value...

Reeza
Super User

@Rahul_SAS wrote:
hi AncaTilea,
why for i=3 var OUT has a valur 2???

This wouldn't happen a data step that was using an input data set. In that case it would reset to missing, because each new line sets all new variables to missing unless you have a retain.

 

However, in your case you have no input data set and are looping so it's really considered a single step and the values are retained until you reset them. 

Reeza
Super User

Comment your code to help understand:

 

data new ; *Create a new dataset called new;
do i=1 to 10; *Loop from 1 to 10;
if mod(i,2)=0 then out=i; *If number is even then assign counter, i, to variable out.
output; *output, since no filters this would be all observations;
end; *end of loop;
run; *end of datastep;

 

Others have already posted the correct solution to your problem.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 8 replies
  • 3282 views
  • 5 likes
  • 4 in conversation