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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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;

Then your code becomes

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

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

Demographer
Pyrite | Level 9

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++.

ballardw
Super User

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;

Reeza
Super User

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?

TomKari
Onyx | Level 15

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 5 replies
  • 1142 views
  • 0 likes
  • 5 in conversation