- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).