I want to create 2 flags: one is if there is change of more than 5 points between recs and another flag if there is no change in score from recs to another 3 consecutive recs.
I am not figuring it out on how to do this and need help.
Required flag1 and flag2 and need something like below
| PID | recs | score | flag1 | flag2 |
| 121 | 1 | 11 | ||
| 121 | 2 | 23 | Y | |
| 121 | 3 | 23 | Y | |
| 121 | 4 | 23 | Y | |
| 121 | 5 | 23 | Y | |
| 122 | 1 | 11 | ||
| 122 | 2 | 14 | ||
| 122 | 3 | 24 | Y | |
| 122 | 4 | 34 | Y | |
| 122 | 5 | 22 | ||
| 122 | 6 | 11 |
in your want should this
| 122 | 2 | 14 |
|
be this
| 122 | 2 | 14 | Y |
It should be as per pid.
/* UNTESTED CODE */
Data want;
set have;
Prev_id=lag(pid);
Prev_id2=lag2(pid);
prev_score=lag(score);
prev_score2=lag2(score);
if score-prev_score>5 and pid=prev_id then flag1='Y';
if score=prev_score and score=prev_score2
and pid=prev_id and pid=prev_id2 then flag2='Y';
run;
to mention is somehow should work for improving and worsening scores. so in the below for 121 it has 9 and decreased the next recs to 2 and it needs to have flag. also if it has same score 3 consecutive times than the second 2 should have flags as Y- excluding the first
| PID | recs | score | flag1 | flag2 |
| 121 | 1 | 3 | ||
| 121 | 2 | 9 | Y | |
| 121 | 3 | 2 | Y | |
| 121 | 4 | 1 | ||
| 122 | 1 | 7 | ||
| 122 | 2 | 1 | Y | |
| 122 | 3 | 3 | ||
| 122 | 4 | 3 | Y | |
| 122 | 5 | 3 | Y |
to mention is somehow should work for improving and worsening scores. so in the below for 121 it has 9 and decreased the next recs to 2 and it needs to have flag.
This is a trivial change to the code I provided. Please see if you can make the modification to the code yourself.
also if it has same score 3 consecutive times than the second 2 should have flags as Y- excluding the first
Sort the data backwards, then if you get a Y in flag2, then add a Y to the next record.
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.