Solved
Contributor
Posts: 50

# Problem to define a chk column

Hi,

Hi have this table:

COD  FLAG

1          N

1          N

1          S

2          N

2          N

3          S

3          N;

I need to create a new table with a new column (chk) that verify the variation of FLAG Variable in the same group of COD coloumn:

if FLAG change in the same group of COD, the variable chk is set to 1 to all group of cod:

here there is the example:

COD  FLAG         CHK

1         N                   1

1         N                   1

1         S                   1

2         N                   0

2         N                   0

3         S                    1

3         N                    1

I create this sas code:

data mytable;

retain _flag;

retain _chk 0;

set a;

by COD;

if first.cod then do; _flag = flag; chk=0; end;

else if _flag ne flag

then do;

_flag=flag;

chk = 1;

end;

run;

it works but i don't get the table that I need.

how can I do that?

Accepted Solutions
Solution
‎10-04-2014 07:53 AM
Posts: 1,270

## Re: Problem to define a chk column

proc sql;

create table want as

select *,case when count(distinct flag)>1 then 1 else 0 end as chk

from mytable

group by cod;

quit;

All Replies
Solution
‎10-04-2014 07:53 AM
Posts: 1,270

## Re: Problem to define a chk column

proc sql;

create table want as

select *,case when count(distinct flag)>1 then 1 else 0 end as chk

from mytable

group by cod;

quit;

Super User
Posts: 10,770

## Re: Problem to define a chk column

```data have;
input COD  FLAG     \$;
cards;
1          N
1          N
1          S
2          N
2          N
3          S
3          N
;
run;
proc sql;
create table want as
select *,case when count(distinct flag) gt 1 then 1 else 0 end as chk
from have
group by cod;
quit;

```

Xia Keshan

Contributor
Posts: 50

## Re: Problem to define a chk column

Thank you,

all of you!!!

Thanks!

🔒 This topic is solved and locked.