BookmarkSubscribeRSS Feed
veblen
Calcite | Level 5
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.
2 REPLIES 2
Patrick
Opal | Level 21
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
veblen
Calcite | Level 5
THANK YOU Patrick!

It worked like a charm.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1611 views
  • 1 like
  • 2 in conversation