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

Hi,

 

I am trying to know whether the character value in different rows in a group of observations is different?

By using Table 1 as an example

obsIDdatelevel
1102/20/2020B
2102/20/2020B
3102/20/2020A
4104/08/2020A
5104/08/2020CCC
6202/20/2020C
7202/20/2020C

The obs 1 to obs 3 belong to the same group because ID and date for them are the same (i.e., ID=1, date=02/20/2020).

Similarly, obs 4 and obs 5 belong to the same group, and obs 6 and obs7 belong to the same group.

 

For these three groups if the value in the level variable is not the same, then I expect to have flag=1, otherwise flag=0.

e.g., for the first group (obs1-obs3), the level of them are different, i.e., level='B' in obs 1 and obs 2, while level='A' in obs 3, then flag of them is 1.

 

I expect to have Table2 like below

obsIDdatelevelflag
1102/20/2020B1
2102/20/2020B1
3102/20/2020A1
4104/08/2020A1
5104/08/2020CCC1
6202/20/2020C0
7202/20/2020C0

 

Could you please give me some suggestions?

Thx in advance.

data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	date :mmddyy10.
	level $
	;
	format
   date mmddyy10.
   ;
	cards;
1,02/20/2020,B 
1,02/20/2020,B 
1,02/20/2020,A
1,04/08/2020,A,
1,04/08/2020,CCC
2,02/20/2020,C
2,02/20/2020,C
	;;;;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	date :mmddyy10.
	level $
	;
	format
   date mmddyy10.
   ;
	cards;
1,02/20/2020,B 
1,02/20/2020,B 
1,02/20/2020,A
1,04/08/2020,A,
1,04/08/2020,CCC
2,02/20/2020,C
2,02/20/2020,C
	;;;;

proc sql;
create table want as
select *, count(distinct level)>1 as flag
from table1
group by id
order by id,date;
quit;

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20
data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	date :mmddyy10.
	level $
	;
	format
   date mmddyy10.
   ;
	cards;
1,02/20/2020,B 
1,02/20/2020,B 
1,02/20/2020,A
1,04/08/2020,A,
1,04/08/2020,CCC
2,02/20/2020,C
2,02/20/2020,C
	;;;;

proc sql;
create table want as
select *, count(distinct level)>1 as flag
from table1
group by id
order by id,date;
quit;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 462 views
  • 2 likes
  • 2 in conversation