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

Hi....I am trying to select data records from a dataset based on the ID and this ID is selected under 2 conditions. Those 2 conditions are if the date is before 20150908, then I only want to select those ID's where the Surcharge is less than 10% of the Cost. Similiarly, for the second condition, if the date is after 20150908, then I only want to select those ID's where the Surcharge is less than %% of the Cost. My question is can I combine these 2 conditions under the same PROC SQL, like have below or should or need to run to PROC SQL and combine the results together after?.....Thanks in advance.

 

 

PROC SQL NOPRINT;

     CREATE TABLE Want AS

                SELECT A.*

                FROM WORK.Summary A

                               LEFT JOIN (SELECT DISTINCT ID

FROM WORK.Summary

(WHERE NUM=’P585’

AND SIN=’02234566’

AND DATE < ‘20150908’

HAVING SURCHARGE < 0.1*COST

ORDER BY ID)

AND

(WHERE NUM=’P585’

AND SIN=’02234566’

AND DATE >= ‘20150908’

HAVING SURCHARGE < 0.05*COST

ORDER BY ID)) B

     ON A.ID=B.ID;

QUIT;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

OK, then what you need is something like

 

PROC SQL;
CREATE TABLE Want AS
SELECT *
FROM Summary
WHERE ID IN
( 	SELECT ID
	FROM Summary
	WHERE 
		NUM=’P585’ AND 
		SIN=’02234566’ AND 
		(	DATE <  '08SEP2015'd AND SURCHARGE < 0.10*COST OR
			DATE >= '08SEP2015'd AND SURCHARGE < 0.05*COST ) );
QUIT;

 or the equivalent if dates are character strings.

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

Although not syntaxically correct, your SQL query seems to be equivalent to

 

PROC SQL;
CREATE TABLE Want AS
SELECT *
FROM Summary
WHERE 
	NUM=’P585’ AND 
	SIN=’02234566’ AND 
	(	DATE <  '08SEP2015'd AND SURCHARGE < 0.10*COST OR
		DATE >= '08SEP2015'd AND SURCHARGE < 0.05*COST );
QUIT;

that is, assuming your dates are real dates and not character strings. If your dates are indeed character strings, you could get away with

 

PROC SQL;
CREATE TABLE Want AS
SELECT *
FROM Summary
WHERE 
	NUM=’P585’ AND 
	SIN=’02234566’ AND 
	(	DATE <  '20150908' AND SURCHARGE < 0.10*COST OR
		DATE >= '20150908' AND SURCHARGE < 0.05*COST );
QUIT;
PG
twildone
Pyrite | Level 9

Hi PG....thank you for your response. I am not too sure if I would get the complete data from your suggestion. The reason I have to select distinct ID's first is that I only want data based on when the Surcharge is less that a certain percent of the cost which changed on a certain date (20150908) for this particular Num and SIN. Once I have a list of distinct ID's based on these condition, I need to go back to the same dataset and extract a complete dataset for only these ID's as they will have other records with different SIN. So really I want to end up with a complete dataset for all the records in the dataset for only these distinct ID's if the 2 conditions are met. Hopefully this clarifies things....Thanks

PGStats
Opal | Level 21

OK, then what you need is something like

 

PROC SQL;
CREATE TABLE Want AS
SELECT *
FROM Summary
WHERE ID IN
( 	SELECT ID
	FROM Summary
	WHERE 
		NUM=’P585’ AND 
		SIN=’02234566’ AND 
		(	DATE <  '08SEP2015'd AND SURCHARGE < 0.10*COST OR
			DATE >= '08SEP2015'd AND SURCHARGE < 0.05*COST ) );
QUIT;

 or the equivalent if dates are character strings.

PG
twildone
Pyrite | Level 9

Thanks PG.....That worked perfectly!!!....

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1104 views
  • 0 likes
  • 2 in conversation