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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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