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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 952 views
  • 1 like
  • 2 in conversation