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

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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