Desktop productivity for business analysts and programmers

Multiple then/else for one IF condition

Reply
Contributor
Posts: 34

Multiple then/else for one IF condition

Hi ! 

 

I tried to do a SAS code with severals THEN and ELSE statement for one IF condition but i can't do it. I tried this code and too without the  ";" in each THEN statement.

 

How i can i do it ? 

 

Thank you very much 

 

DATA TEST ;
	INPUT TYPE V1 V2 V3 V4 AMOUNT ;
		CARDS ;
		A 12 54 65 87 64
		B 85 995 454 78 1
		C 6 8 55 89 12 84 
		D 87 54 8 98 3 14 
		;
RUN ;

DATA TEMP ;
	SET TEST ;
	 IF TYPE IN ("A", "B", "B") THEN 
	 NEW_VBLE1 = V1 ;
	 NEW_VBLE2 = V2 ;
	 NEW_VBLE3 = V3 ;
	 NEW_VBLE4 = V4 ;
	 	ELSE 
			 NEW_VBLE1 = V1*AMOUNT;
			 NEW_VBLE2 = V2*AMOUNT ;
			 NEW_VBLE3 = V3*AMOUNT ;
			 NEW_VBLE4 = V4*AMOUNT ;
RUN ;
Super User
Super User
Posts: 9,840

Re: Multiple then/else for one IF condition

[ Edited ]
Posted in reply to luciacossaro

Please don't code all in uppercase, its like your shouting the code at us.

data temp;
set test;
if type in ("A","B") then do;
new_vble1=v1;
new_vble2=v2;
new_vble3=v3;
new_vble4=v4;
end;
else do;
new_vble1=v1*amount;
...
end;
run;
Trusted Advisor
Posts: 1,327

Re: Multiple then/else for one IF condition

[ Edited ]
Posted in reply to luciacossaro

Hi @luciacossaro,

 

The "do; ... end;" technique suggested by RW9 can also be found in the example section of the IF-THEN/ELSE statement documentation (fifth bullet point).

 

Your first data step will work as soon as you insert a $ sign after "TYPE".

 

Super User
Posts: 10,571

Re: Multiple then/else for one IF condition

Posted in reply to luciacossaro

And if you have more choices to make, use a select statement:

select (type);
  when ('A','B') do;
  /* code */
  end;
  when ('C','D') do;
  /* more code */
  end;
  otherwise do;
  /* still more code */
  end;
end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 34

Re: Multiple then/else for one IF condition

Posted in reply to luciacossaro

Thank you for your answer. Is it possible that the code has an error? Because it do not work correctly, i believe it not consider the line " if type in ("A","B") then do;". I get this result :

 

TYPEV1V2V3V4AMOUNTnew_vble1new_vble2new_vble3new_vble4
A1254658764768345641605568
B859954547818599545478
C6855891272966601068
D8754898326116224294
Super User
Super User
Posts: 9,840

Re: Multiple then/else for one IF condition

Posted in reply to luciacossaro

I assume that is a reply to me?  If so then your type variable does not look like the test data you have provided as:

DATA TEST ;
  INPUT TYPE $ V1 V2 V3 V4 AMOUNT ;
      CARDS ;
      A 12 54 65 87 64
      B 85 995 454 78 1
      C 6 8 55 89 12 84 
      D 87 54 8 98 3 14 
      ;
RUN ;

data temp;
 set test;
 if type in ("A","B") then do;
   new_vble1=v1;
   new_vble2=v2;
   new_vble3=v3;
   new_vble4=v4;
 end;
 else do;
   new_vble1=v1*amount;
 end;
run; 

Works fine.  Its likely you have a space or something, try strip(type) in ...

Ask a Question
Discussion stats
  • 5 replies
  • 93 views
  • 0 likes
  • 4 in conversation