Help using Base SAS procedures

Add a tiny random number to a variable to avoid "Repeated values for a point"

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Add a tiny random number to a variable to avoid "Repeated values for a point"

I'm using Proc G3Grid.  Unfortunately in my data set there are violations of the following:

"If more than one observation in the input data set has the same values for both horizontal variables, x and y, a warning message is printed, and only the first such point is used in the interpolation."

Therefore, I'd like to add a teeny tiny bit of randomness to those variables.

For instance, if the original value is 5, I'd like to change that to 5.0000014738.

etc.

Can someone please show the appropriate code to do this?  I'd like the changes to be "in place," as opposed to creating new variables.

Thanks much.

Nicholas Kormanik


Accepted Solutions
Solution
‎04-15-2013 07:52 PM
Respected Advisor
Posts: 4,644

Re: Add a tiny random number to a variable to avoid "Repeated values for a point"

If you don't mind moving all your points by tiny amounts, you could do :

data want;

call streaminit(7567);

set have;

x = x + (rand("UNIFORM")-0.5) * 0.00001;

run;

If, on the other hand, you want to move only duplicated points, but you know

that such points are consecutive in your dataset, you could do :

data want;

call streaminit(7567);

set have; by x y notsorted;

if not first.y then x = x + (rand("UNIFORM")-0.5) * 0.00001;

run;

PG

PG

View solution in original post


All Replies
Solution
‎04-15-2013 07:52 PM
Respected Advisor
Posts: 4,644

Re: Add a tiny random number to a variable to avoid "Repeated values for a point"

If you don't mind moving all your points by tiny amounts, you could do :

data want;

call streaminit(7567);

set have;

x = x + (rand("UNIFORM")-0.5) * 0.00001;

run;

If, on the other hand, you want to move only duplicated points, but you know

that such points are consecutive in your dataset, you could do :

data want;

call streaminit(7567);

set have; by x y notsorted;

if not first.y then x = x + (rand("UNIFORM")-0.5) * 0.00001;

run;

PG

PG
Regular Contributor
Posts: 212

Re: Add a tiny random number to a variable to avoid "Repeated values for a point"

Thanks PG.  Your first example would be my present case.  I have a follow-up on this.  Please advise:

If I have many columns of data and not just one, and I want to do the same to each and every column, is there a way to do that?

If there is not a shortcut way of changing more than one of the, say, 100 variables, the following is, I suppose, what I'd have to do.

data want;

call streaminit(7567);

set have;

x1 = x1 + (rand("UNIFORM")-0.5) * 0.00001;

x2 = x2 + (rand("UNIFORM")-0.5) * 0.00001;

x3 = x3 + (rand("UNIFORM")-0.5) * 0.00001;

x4 = x4 + (rand("UNIFORM")-0.5) * 0.00001;

x5 = x5 + (rand("UNIFORM")-0.5) * 0.00001;

x6 = x6 + (rand("UNIFORM")-0.5) * 0.00001;

.

.

.

x100 = x100 + (rand("UNIFORM")-0.5) * 0.00001;

run;



Respected Advisor
Posts: 3,124

Re: Add a tiny random number to a variable to avoid "Repeated values for a point"

Since PG is leaving the forum indefinitely, https://communities.sas.com/people/PGStats/activity

I will answer this one for him, not necessarily as good, but hopefully workable:

data want;

call streaminit(7567);

set have;

array _x x1-x100;

   do over _x;

    _x = _x + (rand("UNIFORM")-0.5) * 0.00001;

end;

run;


Haikuo

Regular Contributor
Posts: 212

Re: Add a tiny random number to a variable to avoid "Repeated values for a point"

_xcellent.  I'll give it a try, Haikuo.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 284 views
  • 3 likes
  • 3 in conversation