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.
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;
@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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.