I am running the following command:
proc sort data= rt2 ; by day dayrank ; run ;
data rt2 ; set rt2 ; by day dayrank ;
if users = lag(users) then dayrank = lag(dayrank) ; run;
It is yielding the following results:
# | security | day | users | dayrank |
123 | WB | 02MAY2018 | 6481 | 123 |
124 | BIDU | 02MAY2018 | 6349 | 124 |
125 | OKTA | 02MAY2018 | 6349 | 125 |
126 | MNKD | 02MAY2018 | 6296 | 126 |
Before running the code, the value in line 125 column 4 was 125. The 'dayrank' column is BEST12. Numeric. Why is it changing the 'dayrank' value in line 125 to '.' instead of 124 when OKTA's users are the same as BIDU's on 02MAY2018?
P.S. - I have also run the code where I include "descending users" as part of the sorting, but it results in the same problem.
Never use LAG conditionally. It only puts values into its FIFO queue when it is called, so a conditional call to the function will skip values.
You can use the IFN function to avoid this:
proc sort data=rt2;
by day dayrank;
run;
data rt2;
set rt2;
by day dayrank;
dayrank = ifn(users = lag(users),lag(dayrank),dayrank);
run;
The IFN (and IFC) function executes all arguments before deciding which to use.
Never use LAG conditionally. It only puts values into its FIFO queue when it is called, so a conditional call to the function will skip values.
You can use the IFN function to avoid this:
proc sort data=rt2;
by day dayrank;
run;
data rt2;
set rt2;
by day dayrank;
dayrank = ifn(users = lag(users),lag(dayrank),dayrank);
run;
The IFN (and IFC) function executes all arguments before deciding which to use.
Your solution was the correct solution for the question I asked; thank you!
What is funny is that I discovered that all I had ever needed to do was to use the proc rank statement initially, and then I would have never had the problem I created for myself in the first place! 🙃
proc rank data= rt2 out= test ties= low descending ; by day ; var users ; ranks usersrank; run ;
Nevertheless, thank you for your help!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.