DATA Step, Macro, Functions and more

do loop, how to keep missing observations

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

do loop, how to keep missing observations

Hello everyone, 

 

I am using a do loop to create a counter variable, count. However, the values that needs to be counted is not fully complete. How can I adjust the following code to include missing observations? I want to add a line that makes count=. if time=., and have no rows added if time=., but i couldn't get an if-then statement to work. Instead, the rows where time=. are removed from the dataset. 

 

Thank you!

 

 

data dat2; 
	set dat1; 
	if time ge 0 then do 
		count=1 to round(time, 1);
			output; 
			end;
run;

Accepted Solutions
Solution
‎09-26-2017 03:36 PM
Super User
Posts: 5,518

Re: do loop, how to keep missing observations

Posted in reply to pamplemouse22

Try adding one more statement at the end of the DATA step:

 

else output;

View solution in original post


All Replies
Solution
‎09-26-2017 03:36 PM
Super User
Posts: 5,518

Re: do loop, how to keep missing observations

Posted in reply to pamplemouse22

Try adding one more statement at the end of the DATA step:

 

else output;

Contributor
Posts: 38

Re: do loop, how to keep missing observations

Posted in reply to Astounding

That worked! Thank you Smiley Happy 

 

Respected Advisor
Posts: 3,156

Re: do loop, how to keep missing observations

Posted in reply to pamplemouse22
data dat2; 
	set dat1; 
if time=. then output;
	else if time ge 0 then do 
		count=1 to round(time, 1);
			output; 
			end;
run;
Super User
Super User
Posts: 7,083

Re: do loop, how to keep missing observations

Posted in reply to pamplemouse22

You could just make sure your upper bound is at least 1.

data dat2; 
  set dat1; 
  do count=1 to max(1,round(time, 1));
    output; 
  end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 110 views
  • 1 like
  • 4 in conversation