BookmarkSubscribeRSS Feed
ganandlife
Calcite | Level 5

 

       I am trying put all the sas datastep into proc sql steps. below is the question and the scenario.

       I have one proc sql statement connected to oracle(though i have not mentioned oracle connection below)

 

proc sql;
create table a as select *from new;
quit; 

   Then two proc sort statement based on above dataset a.

proc sort data = a;
by promo descending flag;
run;
proc sort data =a nodupkey out =new1;
by promo;
run;

  Now I want to do these two proc sort statements inside proc sql statement itself. 

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@ganandlife wrote:

 

       I am trying put all the sas datastep into proc sql steps.b


Why? The functions in a data step and the functions in SQL do not completely overlap, some things won't be possible in a data step and some things won't be possible in PROC SQL.

 


   Then two proc sort statement based on above dataset a.
proc sort data = a;
by promo descending flag;
run;
proc sort data =a nodupkey out =new1;
by promo;
run;

  Now I want to do these two proc sort statements inside proc sql statement itself. 


This isn't clear ... it sounds like you want to run PROC SORT inside SQL which isn't possible.

 

You can use the SORT clause in PROC SQL, is that what you mean?

--
Paige Miller
ganandlife
Calcite | Level 5
Nope.....First i need to sort by promo and descending flag, then get the highest flag for each promo and send the remaining to new dataset new 1 –. I want to do all these in single proc sql statement
Reeza
Super User

You’re using pass through so you need Oracle SQL not SAS SQL, correct?

ganandlife
Calcite | Level 5
yes
PaigeMiller
Diamond | Level 26

Sounds like you want to use the HAVING clause in PROC SQL

--
Paige Miller
ganandlife
Calcite | Level 5
I want to have duplicates into new datasets also...
PaigeMiller
Diamond | Level 26

HAVING in PROC SQL can select duplicates if they are present in the data.

--
Paige Miller
Tom
Super User Tom
Super User

The proc sort steps will find the first observation that has the maximum value of FLAG within each group defined by PROMO.

In PROC SQL the best you can do is find ALL observations that have the maximum value of FLAG within each group.

proc sql noprint;
create table new1 as
  select *
  from a
  group by promo
  order by promo
  having flag=max(flag)
;
quit;

In ORACLE or other external databases that have newer implementations of SQL syntax you can use ROW_NUMBER to find the first observation. So something like this.

proc sql noprint ;
connect to oracle ..... ;
create table new1 as
select * from connection to oracle
(select *
 from a
 order by promo
 having 1=row_number() over (partition by promo order by promo, flag desc)
)
;
quit;

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!

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
  • 8 replies
  • 2808 views
  • 1 like
  • 4 in conversation