Assign a value to a Top % of data

Solved
Occasional Contributor
Posts: 12

Assign a value to a Top % of data

Hi guys,

I would like to create a dummy variable 1 for the top 30% of the data (Sorting from lowest to highest value of a variable) and 0 for the rest... For example,

IDValue
150
24
3100
460
51
60.5
716
836
915
105

Then after the procedure, it should be

IDValue
10
21
30
40
51
61
70
80
90
100

What is the fastest way to do it? I do also wanna change from the top 30% to 10%, 20% and so on... Thanks!

Accepted Solutions
Solution
‎05-17-2013 02:27 PM
Posts: 5,543

Re: Assign a value to a Top % of data

The straightforward way would go like this:

data before;

input ID Value;

datalines;

1 50

2 4

3 100

4 60

5 1

6 0.5

7 16

8 36

9 15

10 5

;

proc rank data=before out=temp percent;

var value;

run;

%let top=30;

data after;

set temp;

value = value <= &top;

run;

proc print; run;

PG

PG

All Replies
Solution
‎05-17-2013 02:27 PM
Posts: 5,543

Re: Assign a value to a Top % of data

The straightforward way would go like this:

data before;

input ID Value;

datalines;

1 50

2 4

3 100

4 60

5 1

6 0.5

7 16

8 36

9 15

10 5

;

proc rank data=before out=temp percent;

var value;

run;

%let top=30;

data after;

set temp;

value = value <= &top;

run;

proc print; run;

PG

PG
🔒 This topic is solved and locked.