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-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!

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.

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