turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- changing a percentage (or set #) of a vars observa...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-12-2010 03:01 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to veblen

05-12-2010 09:36 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

05-13-2010 12:06 PM

THANK YOU Patrick!

It worked like a charm.

It worked like a charm.