BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aman4SAS
Obsidian | Level 7
Hi All,
Data x;
Input A B$ ;
Datalines;
101 a
101 b
101 b
101 c
101 d
102 a
102 a
102 a
102 b
102 b
102 c
103 a
103 b
103 c
103 d
103 d
103 e
;
Run;
Required data like:
101 a 1
101 b 2
101 b 2
101 c 3
101 d 4
102 a 1
102 a 1
102 a 1
102 b 2
102 b 2
102 c 3
103 a 1
103 b 2
103 c 3
103 d 4
103 d 4
103 e 5
My code is:
Data new;
Set x;
If first.a then i=1;
Else if b ne lag(b) then i=i+1;
Retain i;
Run;

I am facing problem in 102 2nd row, in 2nd row i is being 2 which should not as b = lag(b)

Please help me
Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You shouldn't use LAG() for this at all, this is better done with BY group processing.
https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

proc sort data=x;
by a b;
run;

data new;
set x;
by A B;
retain i;
if first.a then i=0;
if first.b then i+1;
run;

@Aman4SAS wrote:
Hi All,
Data x;
Input A B$ ;
Datalines;
101 a
101 b
101 b
101 c
101 d
102 a
102 a
102 a
102 b
102 b
102 c
103 a
103 b
103 c
103 d
103 d
103 e
;
Run;
Required data like:
101 a 1
101 b 2
101 b 2
101 c 3
101 d 4
102 a 1
102 a 1
102 a 1
102 b 2
102 b 2
102 c 3
103 a 1
103 b 2
103 c 3
103 d 4
103 d 4
103 e 5
My code is:
Data new;
Set x;
If first.a then i=1;
Else if b ne lag(b) then i=i+1;
Retain i;
Run;

I am facing problem in 102 2nd row, in 2nd row i is being 2 which should not as b = lag(b)

Please help me
Thanks



View solution in original post

6 REPLIES 6
Reeza
Super User

You shouldn't use LAG() for this at all, this is better done with BY group processing.
https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

proc sort data=x;
by a b;
run;

data new;
set x;
by A B;
retain i;
if first.a then i=0;
if first.b then i+1;
run;

@Aman4SAS wrote:
Hi All,
Data x;
Input A B$ ;
Datalines;
101 a
101 b
101 b
101 c
101 d
102 a
102 a
102 a
102 b
102 b
102 c
103 a
103 b
103 c
103 d
103 d
103 e
;
Run;
Required data like:
101 a 1
101 b 2
101 b 2
101 c 3
101 d 4
102 a 1
102 a 1
102 a 1
102 b 2
102 b 2
102 c 3
103 a 1
103 b 2
103 c 3
103 d 4
103 d 4
103 e 5
My code is:
Data new;
Set x;
If first.a then i=1;
Else if b ne lag(b) then i=i+1;
Retain i;
Run;

I am facing problem in 102 2nd row, in 2nd row i is being 2 which should not as b = lag(b)

Please help me
Thanks



Aman4SAS
Obsidian | Level 7
Thanks a lot. If there anyway where i can find my mistake n learn
FreelanceReinh
Jade | Level 19

Hi @Aman4SAS,

 

If you really want to use the LAG function, make sure that it is executed unconditionally:

data new;
set x;
by a;
if b ne lag(b) then i+1;
if first.a then i=1;
run;
Aman4SAS
Obsidian | Level 7
Thanks a lot. Is there anyway where i can find my mistake n learn the problem
Reeza
Super User
Googling "issue with lag" would likely have brought up many posts that illustrate this issue. It's a common misunderstanding with the lag function that once you understand how it works you're fine. Don't forget it now 🙂
FreelanceReinh
Jade | Level 19

See also the description and examples in the documentation of the LAG function. Then be creative and write your own DATA steps using the LAG function (applied to small datasets like SASHELP.CLASS or datasets you create for this purpose), moving from very simple to more advanced little programs. Always examine the output dataset or printed output they may create (and of course the log) and check if the result matches your understanding. If it doesn't, adapt your understanding. Thus you will more and more develop the ability to predict the results of new examples (even tricky ones as in If statement Short circuiting and Lag function).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 844 views
  • 2 likes
  • 3 in conversation