## How do i identify first unique value by ID?

Solved
Occasional Contributor
Posts: 12

# 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:

 ID Revenue Dept Flag (want to code) 1 \$2 a 1 1 \$1 a 0 1 \$0 b 0 1 \$3 c 1 1 \$0 c 0 1 \$5 d 1 1 \$10 d 0 2 \$2 a 1 2 \$1 b 1 2 \$0 c 0 2 \$3 c 1 2 \$0 d 0 3 \$2 a 1 3 \$1 a 0 3 \$0 b 0 3 \$3 a 0 3 \$0 a 0

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: 13,583

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

Posted in reply to lai302120

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

All Replies
Solution
‎08-09-2016 08:16 PM
Super User
Posts: 13,583

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

Posted in reply to lai302120

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: 13,583

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

Posted in reply to lai302120

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?

 ID Revenue Dept Flag (want to code) 1 \$2 a a 1 \$1 a 1 \$0 b 1 \$3 c c 1 \$0 c 1 \$5 d d 1 \$10 d 2 \$2 a a 2 \$1 b b 2 \$0 c c 2 \$3 c 2 \$0 d 3 \$2 a a 3 \$1 a 3 \$0 b 3 \$3 a 3 \$0 a
☑ This topic is solved.

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

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