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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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

lai302120
Calcite | Level 5
Thanks this solution worked perfectly!
just a quick question..why do i need to sort revenue if there is already a "revenue>0" condition?
ballardw
Super User

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.

lai302120
Calcite | Level 5

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 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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