DATA Step, Macro, Functions and more

changing a percentage (or set #) of a vars observations...

Reply
Contributor
Posts: 32

changing a percentage (or set #) of a vars observations...

What is the simplest way to change a percentage of a vars observations in a data set based on a condition on that single var?

For example: I want to change 50% of the observations in a data set where var A = 1 to var A = 2? (50 current obs where A =1 are to be changed to 25 obs where A = 1 and 25 where A = 2.)

Even the ability to change x-number of obs where var A = 1 to A = 2 would work.

For example: 100 obs where A = 1; A is changed to 2

Normally I have the luxury of a second variable to condition against but in this case I don't have anything to use.

Thanks.
Respected Advisor
Posts: 3,890

Re: changing a percentage (or set #) of a vars observations...

If it doesn't have to be exatly 50/50 then you could use ranuni() in the way:

if a=1 then
do;
if ranuni()>0.5 then a=2;
end;

If it has to be exactly 50/50 but doesn't have to be random then you just could count the total number of obs where A=1 in a first pass through the data and then in a second pass set half of these obs where A=1 to A=2.

If it has to be exactly 50/50 but random which record is set to A=2 then it's getting a bit harder.
You would still have to count the total number of obs where A=1 in a first pass through the data but then use a more sophisticated way for assigning 50% of obs where A=1 with A=2.
The approach I have in mind is mainly the same like picking a random sample (only that instead of writing the data to an sample data set you would assign the value 2 to A). This is the link to the code of how to pick the values together with some explanations: http://groups.google.com/group/comp.soft-sys.sas/browse_thread/thread/231bef19aaf0f2b1/fdabf8498967b...

HTH
Patrick
Contributor
Posts: 32

Re: changing a percentage (or set #) of a vars observations...

THANK YOU Patrick!

It worked like a charm.
Ask a Question
Discussion stats
  • 2 replies
  • 171 views
  • 0 likes
  • 2 in conversation