Hi,
I have this table:
id
1
0
0
0
1
0
1
I want to set a flag if any value change is there from the previous one.
as shown below:
id flag
1 N
0 Y
0 N
0 N
1 Y
0 Y
1 Y
here i tried using lag function and compare the value to get flag. Please suggest any other ways to do it.
Thanks
The LAG function is the proper tool for this, use it (Maxim 14).
data have;
input id;
datalines;
1
0
0
0
1
0
1
;
data want;
set have;
flag = ifc(id = lag(id) | _N_ = 1, "N", "Y");
run;
@Aexor alternate solution with the help of proc sql;
data have;
input id;
datalines;
5
0
0
0
1
0
1
;
proc sql noprint;
create table have1 as
select *,monotonic() as c from have;
create table WANT as
select a.id,ifc(a.id eq b.id | a.c=1,'N','Y') as flag from have1 as a left join have1 as b on a.c=b.c+1;
quit;
Thank you
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.