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

Hi everyone, I am a newbie in SAS. 

 

I have a large data set with numerous variables two of which are "ID" and "Trans_amt"

 

What I want to find is to count the number of IDs for which the trans_amt remains same in all observations of that ID. 

 

ID       Trans_amt  

1          1000

1          1000

1          1000

1          1000

2            500

2            500

2            500

3            750

3            750

3            800

4           1200

4           1500

4           1200

 

Example for ID 1 the Trans_amt is 1000 for all 4 cases right? I want to create a new variable ID_1 where it stored the ID of those only. 

 

Thanks a lot in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @paulsud1 

 

I am not sure whether this code could satisfy your request

data have;
	input ID Trans_amt;
	cards;
1 1000
1 1000
1 1000
2 500
2 500
2 500
3 750
3 750
3 800
4 1200
4 1500
4 1200
;
run;

/* Retrieve id for which all Trans_amt are the same */
proc sort data=have;
	by ID Trans_amt;
run;

data ID_list;
	set have;
	by ID;
	if first.id or Trans_amt=lag(Trans_amt) then id_1=1;
	else id_1=0;
	if last.id then output;
	keep id id_1;
run;

/* Merge with original dataset */
proc sort data=ID_list;
	by ID ;
run;

data want;
	merge have ID_list;
	by ID;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

@paulsud1 hi and welcome to the SAS Community 🙂

 

Can you post what you want your desired result to look like? Just like you posted usable example data. And kudos for that

PeterClemmensen
Tourmaline | Level 20

This gives you an indicator variable of 1 if the Trans_amt is constant for the entire group and 0 if it has multiple values

 

data have;
input ID Trans_amt;
datalines;
1 1000
1 1000
1 1000
1 1000
2 500
2 500
2 500
3 750
3 750
3 800
4 1200
4 1500
4 1200
;

proc sql;
    create table want as
    select *, (count (distinct Trans_amt))=1 as ind
    from have 
    group by ID;
quit;
ed_sas_member
Meteorite | Level 14

Hi @paulsud1 

 

I am not sure whether this code could satisfy your request

data have;
	input ID Trans_amt;
	cards;
1 1000
1 1000
1 1000
2 500
2 500
2 500
3 750
3 750
3 800
4 1200
4 1500
4 1200
;
run;

/* Retrieve id for which all Trans_amt are the same */
proc sort data=have;
	by ID Trans_amt;
run;

data ID_list;
	set have;
	by ID;
	if first.id or Trans_amt=lag(Trans_amt) then id_1=1;
	else id_1=0;
	if last.id then output;
	keep id id_1;
run;

/* Merge with original dataset */
proc sort data=ID_list;
	by ID ;
run;

data want;
	merge have ID_list;
	by ID;
run;
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
  • 3 replies
  • 1228 views
  • 1 like
  • 3 in conversation