DATA Step, Macro, Functions and more

How do i identify first unique value by ID?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

How do i identify first unique value by ID?

Hi,

i'm trying to identify the distinct departments where revenue > 0 in the following dataset:

 

IDRevenueDeptFlag (want to code) 
1$2a1
1$1a0
1$0b0
1$3c1
1$0c0
1$5d1
1$10d0
2$2a1
2$1b1
2$0c0
2$3c1
2$0d0
3$2a1
3$1a0
3$0b0
3$3a0
3$0a0

 

I only want to consider rows where REVENUE > 0. I also want to flag within each ID, the first unique department as FLAG = 1.

So, for ID = 1, the total number of unique departments is SUM(FLAG) = 3. 

 

Hope I made sense...please let me know if you need any additional info.

 

Thanks for your help!


Accepted Solutions
Solution
‎08-09-2016 08:16 PM
Super User
Posts: 10,466

Re: How do i identify first unique value by ID?

If you data is sorted by the ID then there is a way to reference the first and last value of a group. The method requires using a BY statement;

For you problem does the existing order of the data matter or would it be okay to sort the data?

If sorting is okay;

proc sort data = have;
   by id dept descending revenue;
run;
 
data want;
   set have;
    by id dept;
    flag = first.dept and revenue>0;
run;

 

  will create a data set with the largest revenue flagged as 1 if greater than 0.

Your g

View solution in original post


All Replies
Solution
‎08-09-2016 08:16 PM
Super User
Posts: 10,466

Re: How do i identify first unique value by ID?

If you data is sorted by the ID then there is a way to reference the first and last value of a group. The method requires using a BY statement;

For you problem does the existing order of the data matter or would it be okay to sort the data?

If sorting is okay;

proc sort data = have;
   by id dept descending revenue;
run;
 
data want;
   set have;
    by id dept;
    flag = first.dept and revenue>0;
run;

 

  will create a data set with the largest revenue flagged as 1 if greater than 0.

Your g

Occasional Contributor
Posts: 12

Re: How do i identify first unique value by ID?

Thanks this solution worked perfectly!
just a quick question..why do i need to sort revenue if there is already a "revenue>0" condition?
Super User
Posts: 10,466

Re: How do i identify first unique value by ID?

The First. values are not conditional. By sorting Revenue descending then the largest Revenue value is associated with the First level of each Dept. Therefore if the first Revenue value is greater than 0 then you want the flag.

Occasional Contributor
Posts: 12

Re: How do i identify first unique value by ID?

Hi Ballard, 

i was wondering if i can get your help again..

 

i want my flag column to show the unique value instead of 1/0. is that easy to do? will the code be the same if i wanted numeric values? 

 

IDRevenueDeptFlag (want to code) 
1$2aa
1$1a 
1$0b 
1$3cc
1$0c 
1$5dd
1$10d 
2$2aa
2$1bb
2$0cc
2$3c 
2$0d 
3$2aa
3$1a 
3$0b 
3$3a 
3$0a 
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 243 views
  • 0 likes
  • 2 in conversation