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
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;
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;
Thank you, worked! can you pls explain what this bit of syntax means:
count(distinct varA)=1 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;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;It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
