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

## Advanced Percentile Questions

Hi,

I have been stuck with this question for quite a while now. I'm new to SAS and the question I'm asking might be quite easy to answer.

Let's say I have two datasets, for examples American Used Cars and Canadian Used Cars. Each dataset contains a variable called price. What I would like to do is to find the percentile for each Price in the Canadian dataset based on all the prices in the American Dataset. Basically I would like to know the price of each used car in Canada was greater than what percent of the prices in the American market.

Any help is very much appreciated.

Erfan

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: Advanced Percentile Questions

Consider this method:

/* Simulate data for American and Canadian Car Prices */
data auc cuc;
call streaminit(876878);
do i = 1 to 10;
price = 10**(4+0.3*rand("NORMAL"));
output auc;
price = 10**(3.5+0.3*rand("NORMAL"));
output auc;
price = 10**(4+0.4*rand("NORMAL"));
output cuc;
end;
run;

/* Generate the percentiles */
proc rank data=auc out=aucr percent; var price; ranks percent; run;
proc rank data=cuc out=cucr percent; var price; ranks percent; run;

/* Merge the datasets */
data ucr;
set aucr cucr INDSNAME=_s;
source = _s;
run;

proc sort data=ucr; by price source; run;

/* Scan the prices, remember the american percentiles as we go */
data want(keep=price percent USpercent);
set ucr;
retain USpercent;
if source = "WORK.AUCR"

then USpercent = percent;
else output;
run;

proc print ; run;

PG

PG
4 REPLIES 4
Super User

## Re: Advanced Percentile Questions

Are you trying to do this by make, model, model year and such?

Calcite | Level 5

## Re: Advanced Percentile Questions

For now just the prices. My main problem is how to find the percentile for a variable based on observations in another dataset.

Data Set 1

10

15

20

25

Data Set 1Data Set 2
1011
1513
2021
25

What I like my program to return for data set 1 is this (percentage of observations in dataset 2 that are less than or equal to each observation in dataset 1)

10->0%

15->66.6%

20->66.6%

25->100%

Opal | Level 21

## Re: Advanced Percentile Questions

Consider this method:

/* Simulate data for American and Canadian Car Prices */
data auc cuc;
call streaminit(876878);
do i = 1 to 10;
price = 10**(4+0.3*rand("NORMAL"));
output auc;
price = 10**(3.5+0.3*rand("NORMAL"));
output auc;
price = 10**(4+0.4*rand("NORMAL"));
output cuc;
end;
run;

/* Generate the percentiles */
proc rank data=auc out=aucr percent; var price; ranks percent; run;
proc rank data=cuc out=cucr percent; var price; ranks percent; run;

/* Merge the datasets */
data ucr;
set aucr cucr INDSNAME=_s;
source = _s;
run;

proc sort data=ucr; by price source; run;

/* Scan the prices, remember the american percentiles as we go */
data want(keep=price percent USpercent);
set ucr;
retain USpercent;
if source = "WORK.AUCR"

then USpercent = percent;
else output;
run;

proc print ; run;

PG

PG
Calcite | Level 5

## Re: Advanced Percentile Questions

Thanks a lot PG. That was really helpful.

Discussion stats
• 4 replies
• 808 views
• 0 likes
• 3 in conversation