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

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 🙂

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;                                                                                                                                         
       
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

3 REPLIES 3
ChrisBrooks
Ammonite | Level 13

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;                                                                                                                                         
       
PeterClemmensen
Tourmaline | Level 20

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;
Astounding
PROC Star

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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