BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ericdrosano
Obsidian | Level 7

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:

 

ericdrosano_1-1605545377723.png

#securitydayusersdayrank
123

WB

02MAY20186481123
124BIDU02MAY20186349124
125OKTA02MAY20186349125
126MNKD02MAY20186296126

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

ericdrosano
Obsidian | Level 7

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!

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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