BookmarkSubscribeRSS Feed
G_I_Jeff
Obsidian | Level 7

Trying to convert the following into one SQL statement, but can't wrap my head around the IF/THEN conditions. Is it possible to do in one SQL statement?

 

DATA TABLE1;

SET TABLE2;

IF OBS1 > 1 THEN

OBS3=((OBS4*.01)*(OBS5*215)) / ......;

ELSE

OBS3=((OBS4*.01)*(OBS5*315)) / ......;

IF OBS1 > 100 THEN OBS1 = 100;

RUN;

 

PROC SORT DATA=TABLE1;

BY OBS1, OBS2;

 

PROC SUMMARY DATA=TABLE1;

BY OBS1, OBS2;

VAR OBS3;

OUTPUT OUT=TABLE3;

MEAN(OBS3)=AVGOBS;

 

I know how to obviously get the PROC SORT and PROC SUMMARY accomplished with SQL. Just need help with the IF/THEN in SQL.

 

PROC SQL;

CREATE TABLE3 AS

SELECT OBS1, OBS2, AVG(OBS3) AS AVGOBS

GROUP BY OBS1, OBS2;

QUIT;

5 REPLIES 5
Steelers_In_DC
Barite | Level 11

It's going to be easier to provide a solution if you give an example of the data you have and what result you are looking for.

Steelers_In_DC
Barite | Level 11
For if then look up pl/sql decode() or case statements.
Reeza
Super User
IF/THEN convert most easily to CASE statements in SQL.
G_I_Jeff
Obsidian | Level 7

Correct. But can I put a CASE statement in front of an AVG(observation) AS xxxx?

 

I want to calculate an average for one observation:

 

AVG(IF observation1 > 1 THEN observation2=observation2*2 ELSE observation2=observation2*3) AS new-observation;

 

I want observation2 calulated to new value before it's averaged. 

Steelers_In_DC
Barite | Level 11

If I understand your question correctly then yes.  Put a comma in front and behind the case statement as if it were any other item you are selecting from the dataset.

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 946 views
  • 0 likes
  • 3 in conversation