BookmarkSubscribeRSS Feed
luciacossaro
Obsidian | Level 7

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 ;
5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
FreelanceReinh
Jade | Level 19

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".

 

Kurt_Bremser
Super User

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;
luciacossaro
Obsidian | Level 7

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 ...

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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