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

Hi there,


I have a dataset that looks like this

 

ID    varA   

1       0

1       0

2       1

2       1

3       0

3       1

 

I want it to look like this (i.e. flag=1 if all values of varA are the same within ID by group):

 

ID    varA   flag

1       0        1

1       0        1

2       1        1

2       1        1

3       0        0

3       1        0


How would I do this? Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID varA;
datalines;
1 0
1 0
2 1
2 1
3 0
3 1
;

proc sql;
   create table want as
   select *, count(distinct varA)=1 as flag 
   from have
   group by ID;
quit;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID varA;
datalines;
1 0
1 0
2 1
2 1
3 0
3 1
;

proc sql;
   create table want as
   select *, count(distinct varA)=1 as flag 
   from have
   group by ID;
quit;
KPCklebspn
Obsidian | Level 7

Thank you, worked! can you pls explain what this bit of syntax means:

count(distinct varA)=1 
PaigeMiller
Diamond | Level 26
proc summary data=have nway;
    class id;
    var vara;
    output out=stats std=std;
run;

data want;
    merge have stats;
    by id;
    if std=0 then flag=1;
    drop std;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20
data have;
input ID varA;
datalines;
1 0
1 0
2 1
2 1
3 0
3 1
;
proc sql;
   create table want as
   select *, std(vara)=0 as flag 
   from have
   group by ID;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 864 views
  • 0 likes
  • 4 in conversation