BookmarkSubscribeRSS Feed
Aexor
Lapis Lazuli | Level 10

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

4 REPLIES 4
Aexor
Lapis Lazuli | Level 10
Thanks. yes I tried with lag function only. just wanted to check is there any other way to compare values in variable?
Can we run a loop , array ? please suggest
PeterClemmensen
Tourmaline | Level 20
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;
singhsahab
Lapis Lazuli | Level 10

@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 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 4 replies
  • 1622 views
  • 3 likes
  • 4 in conversation