BookmarkSubscribeRSS Feed
Meghana3
Calcite | Level 5

Need help fixing this code

 

%macro random_number(seed,random_number);
     /**********************************************************************
     ****This macro uses seed to produce a random number between 0 and 1****
     ****The random number will be in a macro variable named in         ****
     ****the argument random_number.                                    ****
     **********************************************************************/

     data _null_;
     random_number = ranuni(&seed);
     call symput('&random_number', strip(random_number));
     stop;
     run;

     %put The random number is &random_number;

     %mend;

     %random_number(12345677,my_rand);
     %put The random number is &&&my_rand;

5 REPLIES 5
kiranv_
Rhodochrosite | Level 12

instead of saying fix this code, if you can write/mention the issue/problem then someone can easily fix it.

 

Thanks

Kiran

Reeza
Super User

Macro variables only resolve in double quotes, not single quotes, and you don't need multiple & in this usage. You also can't have a macro variable named as a numeric so that would be problematic, CALL


The first argument to CALL SYMPUT should be the macro variable name, which cannot be the same as the value in this case. 

And you should use CALL SYMPUTX instead as well. 

That's my guess at your issue, you didn't really say what's wrong and I didn't run your code either. 

 


@Meghana3 wrote:

Need help fixing this code

 

%macro random_number(seed,random_number);
     /**********************************************************************
     ****This macro uses seed to produce a random number between 0 and 1****
     ****The random number will be in a macro variable named in         ****
     ****the argument random_number.                                    ****
     **********************************************************************/

     data _null_;
     random_number = ranuni(&seed);
     call symput('&random_number', strip(random_number));
     stop;
     run;

     %put The random number is &random_number;

     %mend;

     %random_number(12345677,my_rand);
     %put The random number is &&&my_rand;


 

art297
Opal | Level 21

I think that the following does what you want:

%macro random_number(seed,random_number);
     /**********************************************************************
     ****This macro uses seed to produce a random number between 0 and 1****
     ****The random number will be in a macro variable named in         ****
     ****the argument random_number.                                    ****
     **********************************************************************/

     data _null_;
     random_number = ranuni(&seed);
     call symputx("&random_number.", random_number, 'G');
     stop;
     run;

     %mend;

%random_number(12345677,my_rand);
%put The random number is &my_rand;

Art, CEO, AnalystFinder.com

 

Tom
Super User Tom
Super User

You could use the %SYSFUNC() macro function and create a "function style" macro instead.

%macro random_number2(seed);
%local value;
%let value=%sysfunc(ranuni(&seed),best32.);
&value.
%mend;

%let my_rand=%random_number2(12345677);
%put The random number is &my_rand;
art297
Opal | Level 21

Regardless of which style macro you choose, do realize that using the ranuni function via a macro will ALWAYS return the first number in that seed .. i.e., NOT a random number.

 

e.g., run the following code and compare the output files (i.e., dontwant and want):

 

%macro random_number2(seed);
  %local value;
  %let value=%sysfunc(ranuni(&seed),best32.);
  &value.
%mend;

data dontwant;
  do i=1 to 100;
    x=%random_number2(0);
    output;
  end;
run;

data want;
  do i=1 to 100;
    x=ranuni(0);
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1110 views
  • 1 like
  • 5 in conversation