BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lansoprazole
Obsidian | Level 7

Hi, 

I am trying to create a random number  and then assign it to a group according to its value.

I used very simple if-then, but it did not work!

 

this is my code:

data ranpatients;
seed=6789;
do i=1 to 40;
x1=int(ranuni(seed)*10000);
x=put(x1,z4.);
output;
end;
if x1 gt 5000 then group = "A"; else group = "B";
run;

When I run it, I have empty group column.

 

Any explanation ? 

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The order of statements is wrong.

 

You have implemented an algorithm that is similar to:

  • Write letter.
  • Mail letter.
  • Apply stamp to letter.

It is hard to put the stamp on the letter after it is already in mailbox.

 

data ranpatients;
  seed=6789;
  do i=1 to 40;
    x1=int(ranuni(seed)*10000);
    x=put(x1,z4.);
    if x1 gt 5000 then group = "A"; else group = "B";
    output;
  end;
run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

The IF statement has to go before the OUTPUT statement.

 

Otherwise (the way you have it), the record is output to the data set RANPATIENTS before GROUP is assigned a value, so GROUP is always missing.

--
Paige Miller
lansoprazole
Obsidian | Level 7
Thanks.

I tried it but the first observation (raw) was not assigned any group.

I will try it again.
PaigeMiller
Diamond | Level 26

@lansoprazole wrote:
Thanks.

I tried it but the first observation (raw) was not assigned any group.

I will try it again.

I tried it too, and group is assigned to every observation in the data set. If it is not working for you, SHOW US the code.

--
Paige Miller
lansoprazole
Obsidian | Level 7
It works now. I had to restart SAS
Tom
Super User Tom
Super User

The order of statements is wrong.

 

You have implemented an algorithm that is similar to:

  • Write letter.
  • Mail letter.
  • Apply stamp to letter.

It is hard to put the stamp on the letter after it is already in mailbox.

 

data ranpatients;
  seed=6789;
  do i=1 to 40;
    x1=int(ranuni(seed)*10000);
    x=put(x1,z4.);
    if x1 gt 5000 then group = "A"; else group = "B";
    output;
  end;
run;
lansoprazole
Obsidian | Level 7
Thanks.
so output should be always at the end? or because I am using a variable from the loop?
Tom
Super User Tom
Super User

If a data step dos not have an explicit OUTPUT statements then SAS will automatically OUTPUT (write the current values to the saved dataset) at the end of the step.

But your code has an OUTPUT statement so there is no extra output added.

 

If you want the value of GROUP to depend on the value of X you have to calculate GROUP before you write the data to the file.

PaigeMiller
Diamond | Level 26

@lansoprazole wrote:
Thanks.
so output should be always at the end? or because I am using a variable from the loop?

I don't know if it should ALWAYS be at the end, as there may be examples where OUTPUT is not at the end.


What you are being advised to do is assign the value of GROUP before the OUTPUT.

--
Paige Miller
Ron_Cody
Obsidian | Level 7
Hi.

I see you are using the RANUNI function. Take a look at the RAND function that replaces EVERY previous random function. You set the seed with "Call Streaminit(seed);" and to get uniform random numbers, you use "Variable = rand('Uniform');". The beauty of this function is that by changing the first argument you can generate normal, Bernoulli, Poisson, etc. You can also set a mean and standard deviation for normal distributions. Take a look. I think you will like this function. SAS recommends it and says it generates "better" random strings.
Kurt_Bremser
Super User

Apart from moving the IF, you should also consider using the more modern (and recommended) RAND function:

data ranpatients;
call streaminit(6789);
do i = 1 to 40;
  x1 = rand("integer",1,9999);
  if x1 gt 5000 then group = "A"; else group = "B";
  x = put(x1,z4.);
  output;
end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 983 views
  • 11 likes
  • 5 in conversation