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