BookmarkSubscribeRSS Feed
prabha8934
Calcite | Level 5

data covg_overlap_flag_2019;
set covg_rec_2019_sorted (keep=POIDS);
by POIDS;
merge covg_rec_2019_sorted
covg_rec_2019_sorted (firstobs=2 keep=BNFT_CVG_EFFECTIVE_DT BNFT_CVG_TERM_DT rename=(BNFT_CVG_EFFECTIVE_DT=next_sd)
rename=(BNFT_CVG_TERM_DT=next_ed));
if (first.POIDS=0 and BNFT_CVG_EFFECTIVE_DT<lag(BNFT_CVG_TERM_DT)) or
(BNFT_CVG_TERM_DT>next_sd and last.POIDS=0) then flag+1;
else
if (first.POIDS=0 and BNFT_CVG_EFFECTIVE_DT=lag(BNFT_CVG_EFFECTIVE_DT))
or (first.POIDS=0 and BNFT_CVG_TERM_DT=lag(BNFT_CVG_TERM_DT))
then flag+1;

else if (last.POIDS=0 and BNFT_CVG_EFFECTIVE_DT=next_sd) or
(BNFT_CVG_TERM_DT=next_ed and last.POIDS=0)
then flag+1;
else flag=0;
run;

6 REPLIES 6
ballardw
Super User

Since SQL really doesn't have much of a concept of reading records by order anything that uses First. or Last. constructs in a data step is going to require a lot of not trivial SQL to get an equivalent. And that is a maybe.

 

Use of LAG and DIF functions is right out. You would need a separate ordered join to get each "lag" value.

 

If that works why would want to replace it?

prabha8934
Calcite | Level 5

Thank you. That's true SQL doesn't have many features like SAS

 

 

smantha
Lapis Lazuli | Level 10

Some SQL languages support lag and (may be dif) but I haven't seen a construct where first. and last. exist. Things that come close to this behavior is using a windowing function and creating a non tied rank. Us teh rank so obtained in the processing

Reeza
Super User
What flavour of SQL? PL/SQL or cursor logic may help but it's painful.
prabha8934
Calcite | Level 5

T-SQL

SASKiwi
PROC Star

IMHO, don't use SQL for purposes it wasn't designed for. The code you've supplied isn't easily translatable into T-SQL. What is wrong with continuing to process in SAS and then load the results into the database?

sas-innovate-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 6 replies
  • 2621 views
  • 3 likes
  • 5 in conversation