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

Hello lads,

 

I have a quick question, I have built a cumulative column which sums up the percentages until it reaches 100. Now what I want to do is to make a new column named RANKING, which only puts a 1 as a value for the first N rows who together/combined reach a minimum of 75%. Everything else has to stay blank.

 

So like this:

 

DATA HAVE

ID -- Location -- Section -- Date -- - Type -- perc% --Cuml.perc%

01 -- Germany -- BB         --1/1/20 --  VW --   20%  --20%

02 -- Germany -- AA         --1/1/20 --  AU --    20%  --40%

03 -- Germany -- AA         --1/1/20 --  AU --    30%  --70%

04 -- Germany -- BB         --1/1/20 --  AU --    10%  --80%

05 -- Germany -- BB         --1/1/20 --  MC --   10%  --90%

06 -- Germany -- BB         --1/1/20 --  VW --   10%  --100%

07 -- Germany -- AA         --2/1/20 --  OP --    25%   --25%

08 -- Germany -- AA         --2/1/20 --  AU --    40%   --65%

09 -- Germany -- BB         --2/1/20 --  MC --   20%   --85%

10 -- Germany -- BB         --2/1/20 --  MC --   5%     --90%

11 -- Germany -- AA         --2/1/20 --  OP --   5%     --95%

12 -- Germany -- AA         --2/1/20 --  VW --   5%    --100%

 

DATA WANT:

ID -- Location -- Section -- Date -- - Type -- perc% --Cuml.perc% --RANKING

01 -- Germany -- BB         --1/1/20 --  VW --   20%  --20%                   --  1

02 -- Germany -- AA         --1/1/20 --  AU --    20%  --40%                   --  1                        

03 -- Germany -- AA         --1/1/20 --  AU --    30%  --70%                   --  1 

04 -- Germany -- BB         --1/1/20 --  AU --    10%  --80%                   --  1

05 -- Germany -- BB         --1/1/20 --  MC --   10%  --90%                    --   

06 -- Germany -- BB         --1/1/20 --  VW --   10%  --100%                 --  

07 -- Germany -- AA         --2/1/20 --  OP --    25%   --25%                  --   1

08 -- Germany -- AA         --2/1/20 --  AU --    40%    --65%                  --   1

09 -- Germany -- BB         --2/1/20 --  MC --   20%   --85%                    --  1

10 -- Germany -- BB         --2/1/20 --  MC --   5%      --90%                   --   

11 -- Germany -- AA         --2/1/20 --  OP --   5%     --95%                     -- 

12 -- Germany -- AA         --2/1/20 --  VW --   5%    --100%                    --

 

I hope its clear guys, I appreciate your help. Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
kelxxx
Quartz | Level 8
data want;
set have;
ranking=lag(cumlperc);
if cumlperc le 0.75 then ranking=1;
else if ranking lt 0.75 then ranking=1;
else ranking=.;
run;

I hope this can help you

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

 

data want;
set have;
by location date;
retain ranking;
if first.date then ranking = 1;
output;
if cum_perc > 75 then ranking = .;
run;

Untested, for lack of usable data.

AK100
Pyrite | Level 9

Thanks Kurt,

I think I can use this one if I change a few things, for now it says 'by variables not properly sorted'.

kelxxx
Quartz | Level 8
data want;
set have;
ranking=lag(cumlperc);
if cumlperc le 0.75 then ranking=1;
else if ranking lt 0.75 then ranking=1;
else ranking=.;
run;

I hope this can help you

AK100
Pyrite | Level 9
Thanks, this worked perfectly
AK100
Pyrite | Level 9

Hello @Kurt_Bremser and @kelxxx 

 

The code you gave me worked perfectly until I got a first variable who directly was greater then or equal to 0.75. That one did not get a ranking 1. It only gets a ranking when the first codes few coded together reach 0.75 but like I said now I have one who directly reaches 0.75+. This is the code I used right now, but it doesnt work: 

data matselectie1;
	set percentage_count;
	Selectie=lag(totpercentage);

	if totpercentage le 0.75 then
		Selectie=1;
	else if FIRST.totpercentage=1 ge 0.75 then
		Selectie=1;
	else if Selectie lt 0.75 then
		Selectie=1;
	else Selectie=.;
run;

 

sas-innovate-2024.png

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.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 759 views
  • 5 likes
  • 3 in conversation