BookmarkSubscribeRSS Feed
indox
Obsidian | Level 7

Hello,

 

%let gen=(rand('uniform'));
%put %sysfunc(compress(&gen,%str(%')));

This is my code above. I want the second line to execute the %put rand(uniform) and give me a number instead of returning then string "(rand(uniform))".

 

Thanks for help.

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hello @indox,

 

Do you want repeated %PUT statements to write the same number (once it has been created with the %LET statement) as shown in the log below

879  %let gen=%sysfunc(rand(uniform));
880  %put &gen;
0.90237695327959
881  %put &gen;
0.90237695327959

or different numbers?

883  %macro newgen; %sysfunc(rand(uniform)); %mend newgen;
884  %put %newgen;
0.68273608107119
885  %put %newgen;
0.54702847357839
indox
Obsidian | Level 7
Hi, thanks for your answer.
But i really want to keep gen value as rand('uniform') -> With single quotes
and use compress when executing it in the %put statement.

Thanks
Kurt_Bremser
Super User

@indox wrote:
Hi, thanks for your answer.
But i really want to keep gen value as rand('uniform') -> With single quotes
and use compress when executing it in the %put statement.

Thanks

In your first post you stated that you want numbers. Numbers do not contain quotes.

Quentin
Super User

Hi,

 

Your requirements are confusing.  You should not need to remove quotes on the %PUT statement.

 

The main problem here is that the quote marks are not part of the macro language, and you have an extra set of parentheses.  If you want to store the code to generate a random number in a macro variable, the easiest way would be to change the value of the macro variable so that the code can be executed by %sysfunc:

 

1    %let gen=rand(uniform) ; *This stores code in a macro variable ;
2    %put &=gen ;
GEN=rand(uniform)
3    %put %sysfunc(&gen) ;    *This uses sysfunc to execute the stored code ;
0.53809128166176
4    %put %sysfunc(&gen) ;
0.37165963975712

If you cannot change the value of gen for some reason, then as others have pointed out, you can create a new macro var with the correct code:

1    %let gen=(rand('uniform'));  *This value has unwanted quotes and extra set of parentheses ;
2    %put &=gen ;
GEN=(rand('uniform'))
3
4    *remove the parentheses ;
5    %let gen2=%sysfunc(substr(&gen,2,%sysfunc(length(&gen))-2)) ;
6    %put &=gen2 ;
GEN2=rand('uniform')
7
8    *remove the quotes ;
9    %let gen3=%sysfunc(compress(&gen2,%str(%')));
10   %put &=gen3 ;
GEN3=rand(uniform)
11
12   %put %sysfunc(&gen3);
0.639510035282
13   %put %sysfunc(&gen3);
0.41172609385102
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
FreelanceReinh
Jade | Level 19

You could use a second macro variable to avoid nested %SYSFUNC calls that don't work as you intended:

14   %let gen=rand('uniform');
15   %let gen2=%sysfunc(compress(&gen,%str(%')));
16   %put %sysfunc(&gen2);
0.16552548436447
17   %put %sysfunc(&gen2);
0.13993664132431
Patrick
Opal | Level 21

Happy macro galore

/* step 1 */
%put %sysfunc(rand(uniform));

/* step 2 */
%let gen=rand(uniform);
%put %sysfunc(&gen);

/* step 3 */
%put %sysfunc(compress(%sysfunc(&gen)));

/* step 4 */
%put %nrbquote(')%sysfunc(compress(%sysfunc(&gen)))%nrbquote(');
%put %unquote(%nrbquote(')%sysfunc(compress(%sysfunc(&gen)))%nrbquote('));

/* alternative */
%let gen=rand(uniform);
%put %unquote(%cmpres(%nrbquote(')%sysfunc(&gen)%nrbquote(')));

Log

31         /* step 1 */
32         %put %sysfunc(rand(uniform));
0.97763167554512
33         
34         /* step 2 */
35         %let gen=rand(uniform);
36         %put %sysfunc(&gen);
0.39544295193627
37         
38         /* step 3 */
39         %put %sysfunc(compress(%sysfunc(&gen)));
0.89240621402859
40         
41         /* step 4 */
42         %put %nrbquote(')%sysfunc(compress(%sysfunc(&gen)))%nrbquote(');
'0.25708963023498'
43         %put %unquote(%nrbquote(')%sysfunc(compress(%sysfunc(&gen)))%nrbquote('));
'0.006260557333'
44         
45         /* alternative */
46         %let gen=rand(uniform);
47         %put %unquote(%cmpres(%nrbquote(')%sysfunc(&gen)%nrbquote(')));
'0.15496678999625'
48         

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1110 views
  • 1 like
  • 5 in conversation