DATA Step, Macro, Functions and more

Help with do loop

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

Help with do loop

Hey everyone, I have written some codes to calculate the P_margin for 4 different years. the code and the results are shown below. But I would like to have S for year 1 eq to 1. How can I modify this code ?

 

Thanx so much Smiley Happy

Capture.PNG

 

 

data B;                                                                                                                                      
   S=1;                                                                                                                                      
   P=0.02;                                                                                                                                   
      do year=1 to 4;                                                                                                                        
        S=S*(1-P);                                                                                                                           
        P_marg=S*P;                                                                                                                          
      output;                                                                                                                                
 end;                                                                                                                                        
run;                                                                                                                                         
       

Accepted Solutions
Solution
‎09-19-2017 05:03 AM
Super Contributor
Posts: 440

Re: Help with do loop

Try this

 

data B;                                                                                                                                      
	S=1;                                                                                                                                      
	P=0.02;  
   
	do year=1 to 4;  
		if year=1 then s=1;
		else S=S*(1-P);                                                                                                                           
		P_marg=S*P;   
		output;
	end;                                                                                                                                        
run;                                                                                                                                         
       

View solution in original post


All Replies
Solution
‎09-19-2017 05:03 AM
Super Contributor
Posts: 440

Re: Help with do loop

Try this

 

data B;                                                                                                                                      
	S=1;                                                                                                                                      
	P=0.02;  
   
	do year=1 to 4;  
		if year=1 then s=1;
		else S=S*(1-P);                                                                                                                           
		P_marg=S*P;   
		output;
	end;                                                                                                                                        
run;                                                                                                                                         
       
PROC Star
Posts: 755

Re: Help with do loop

How about simply

 

data B;
   S=1;P=0.02;Year=1;P_marg=S*P;output;
      do year=2 to 4;
        S=S*(1-P);
        P_marg=S*P;
      output;
 end;
run;
Super User
Posts: 5,513

Re: Help with do loop

There's another way to interpret your request.  It's possible you want exactly the same numbers you got from your original program, but with S=1 on the first observation.  For that:

 

data B;

  S=1;

  P=0.02;

  do year=1 to 4;

     S=S*(1-P);

     if year=1 then replacement_S=1;

     else replacement_S = S;

     P_marg = S * P;

     output;

  end;

  drop S;

  rename replacement_S = S;

run;

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 133 views
  • 6 likes
  • 4 in conversation