## Recode with probabilities

Solved
Frequent Contributor
Posts: 124

# Recode with probabilities

Hi,

Suppose that probm1-probm10 are cumulative probabilities where probm10=1. The variable MUNIC should be recode following randomly assuming those probabilities. For this purpose, I write thoses lines:

u=rand('uniform');

if u<probm1 then MUNIC=1;

if probm1<=u<probm2 then MUNIC=2;

if probm2<=u<probm3 then MUNIC=3;

if probm3<=u<probm4 then MUNIC=4;

if probm4<=u<probm5 then MUNIC=5;

if probm5<=u<probm6 then MUNIC=6;

if probm6<=u<probm7 then MUNIC=7;

if probm7<=u<probm8 then MUNIC=8;

if probm8<=u<probm9 then MUNIC=9;

if probm9<=u<probm10 then MUNIC=10;

My question: is there a more simple way to do it? With 10 categories, this isn't so huge, but I'll need to do the same with over 100 categories and thus, I don't think I did the best way.

Accepted Solutions
Solution
‎12-21-2012 05:06 PM
Super User
Posts: 13,550

## Re: Recode with probabilities

If your solution in C++ involved arrays then thats one way in SAS. Some of the code below is incase your upper variable count changes so if you only had probm73 for example it will stop there.

array p {*} probm1 - probm100;

do _i_ = 1 to (dim(p)-1);

if _i_ = 1 and u < p[_i_] then Munic=_i_;

else if p[_i_] <= u < p[_i_ + 1] then Munic=_i_;

end;

All Replies
Super User
Posts: 8,114

## Re: Recode with probabilities

Why not use a FORMAT?

If you had the cutoffs as macro variables you could create it this way.

proc format ;

value munic

0 - &prob1 = 1

&prob1 - &prob2 = 2

....

&prob9 - &prob10 = 10

other = .

;

run;

munic = input(put(u,munic.),best.);

You can also build the format from a dataset using the CNTLIN option of PROC FORMAT.

Frequent Contributor
Posts: 124

## Re: Recode with probabilities

I don't think it's possible in my case, because variables probm1-probm10 are created following a big set of macros.

Also, the problem is the same if I have 100 categories. I try to find a way to write this in just some lines and not a hundred. I know it's possible in C++.

Solution
‎12-21-2012 05:06 PM
Super User
Posts: 13,550

## Re: Recode with probabilities

If your solution in C++ involved arrays then thats one way in SAS. Some of the code below is incase your upper variable count changes so if you only had probm73 for example it will stop there.

array p {*} probm1 - probm100;

do _i_ = 1 to (dim(p)-1);

if _i_ = 1 and u < p[_i_] then Munic=_i_;

else if p[_i_] <= u < p[_i_ + 1] then Munic=_i_;

end;

Super User
Posts: 23,733

## Re: Recode with probabilities

If you have the categories already in a dataset then you can use proc format with cntlin and then apply the format, you don't have to type out the boundaries. What does your original data look like from the macro?

PROC Star
Posts: 1,312

## Re: Recode with probabilities

Give this a try:

seed = 123;

call rantbl(seed, .05, .2, .6, .1, result);

You should get 5% 1's, 20% 2's, 60% 3's, 10% 4's, and the residue (5)% 5's in result.

Tom

🔒 This topic is solved and locked.