BookmarkSubscribeRSS Feed
raja777pharma
Fluorite | Level 6

Hello ,

 

I am try to write code in Proc sql for below data step , but i am not getting as results in data step vs proc sql.

 

My data step:

data last_ass_dt;
  set all_results;
  by usubjid rsdt;
  if first.usubjid;
keep usubjid rsdt;
run;

My testing proc sql code:

 

proc sql;
   create table last_ass_dt as
   select usubjid,rsdt,'last Assesment date' as ByGroup
   from all_results a1
   where rsdt = (select min(rsdt)
                     from all_results a2
                     where a1.rsdt = a2.rsdtc)
  order by usubjid,rsdt;
quit;

please let me know what i am missing in PROC SQL code.

 

Thank you,

Raja.

 

 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Why not just do this in the Data Step? Seems much easier.

 

However, try this (untested)

 

proc sql;
   create table last_ass_dt as
   select usubjid, rsdt
   from all_results
   group by usubjid
   having min(rsdt) = rsdt;
quit;
PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:

Why not just do this in the Data Step? Seems much easier.

 

However, try this (untested)

 

proc sql;
   create table last_ass_dt as
   select usubjid, rsdt
   from all_results
   group by usubjid
   having min(rsdt) = rsdt;
quit;

MIN is not the same as first.variable (and I don't know if it works for character variables)

 

To @raja777pharma , this is a feature that exists only in a DATA step, so you really need to use a DATA step. There is really no equivalent feature in SQL.

--
Paige Miller
ballardw
Super User

SQL is based on SET operators and as such really doesn't have a native concept of "first" or "last" for values in a subset.

If you did around the internet you will find that the order of records in a query can be seriously changed by code optimizers working in the background to make a query execute more efficiently.

So if you get something that works one time for SQL it may not the next time with different data.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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