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!
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
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
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.
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 |
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.