BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rakeon
Quartz | Level 8

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?

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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;

View solution in original post

3 REPLIES 3
stat_sas
Ammonite | Level 13

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;

Ksharp
Super User
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

Rakeon
Quartz | Level 8

Thank you,

all of you!!!

Thanks!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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