Passing Real Numbers as Macro Variables

From sasCommunity
Jump to: navigation, search

Using SymputX to Convert Real Numbers to Macro Variables

http://tinyurl.com/yes6hr5 for this page:

http://www.sascommunity.org/wiki/Passing_Real_Numbers_as_Macro_Variables

DATA _Null_;
do Denominator = 1 to 19;
   Real = 1/Denominator;
   call symputx(catt('Real',Denominator)
               ,put(Real,hex16.)
               );
   call execute(catt('%nrstr(%Put Real'
                    ,Denominator,':&Real'
                    ,Denominator,
                    ';)'));
   end;
stop;
run;

log:

NOTE: CALL EXECUTE generated line.
1         +  %Put Real1:&Real1;
Real1:3FF0000000000000
2         +  %Put Real2:&Real2;
Real2:3FE0000000000000
3         +  %Put Real3:&Real3;
Real3:3FD5555555555555

... [snippage occureth]

17        +  %Put Real17:&Real17;
Real17:3FAE1E1E1E1E1E1E
18        +  %Put Real18:&Real18;
Real18:3FAC71C71C71C71C
19        +  %Put Real19:&Real19;
Real19:3FAAF286BCA1AF28

Demo Passing Real Numbers in hex16 as Macro Variables

*name: demo-passing-real-numbers-as-mvars;
 
options mprint;*testing;
 
PROC Freq data   = sashelp.class;
          tables   Sex
                 / list missing
             out = Work.Freq_Class; 
 
PROC SQL; describe table Work.freq_class;
          quit;
 
PROC Print data = Work.freq_class;
           title2 Work.freq_class;
           format _numeric_ hex16.;
 
%macro PrintThis
      (data    = sashelp.class
      ,var     = sex
      ,sex     =
      ,count   =
      ,percent =);
 
DATA _Null_;
call symputx('Real_count'  ,input("&Count"  ,hex16.));
call symputx('Real_percent',input("&Percent",hex16.));
stop;
 
proc print data=&Data.(where = (&Var. eq "&&&Var."));
title2 "data:&Data. &Var. eq &&&Var.";
title3 Count   hex : &Count.;
title4 Percent hex : &Percent.;
title5 Count   real: &Real_Count.;
title6 Percent real: &Real_Percent.;
title7 Count   real: %sysfunc(inputn(&Count.,hex16.));
title8 Percent real: %sysfunc(inputn(&Percent.,hex16.));
title9 Percent real: %sysfunc(round(%sysfunc(inputn(&Percent.,hex16.)),.1));
run;
%mend;
 
DATA _Null_;
attrib Stmnt length = $128; 
do until(EndoFile);
   set Library.freq_class end = EndoFile;
   Stmnt = catt('%PrintThis(sex=',Sex
               ,',count='  ,put(Count  ,hex16.)
               ,',percent=',put(Percent,hex16.)
               ,')');
   putlog Stmnt=;
   call execute(catt('%nrstr(',Stmnt,')'));
   end;
stop;
run;

listing:

The FREQ Procedure
                                Cumulative    Cumulative
Sex    Frequency     Percent     Frequency      Percent
--------------------------------------------------------
F             9       47.37             9        47.37
M            10       52.63            19       100.00


Work.freq_class

Obs    Sex               COUNT             PERCENT

 1      F     4022000000000000    4047AF286BCA1AF3
 2      M     4024000000000000    404A50D79435E50E


data:sashelp.class sex eq F
Count   hex : 4022000000000000
Percent hex : 4047AF286BCA1AF3
Count   real: 9
Percent real: 47.368421053
Count   real: 9
Percent real: 47.3684210526315
Percent real: 47.4

Obs    Name       Sex    Age    Height    Weight

  2    Alice       F      13     56.5       84.0
  3    Barbara     F      13     65.3       98.0
  4    Carol       F      14     62.8      102.5

data:sashelp.class sex eq M
Count   hex : 4024000000000000
Percent hex : 404A50D79435E50E
Count   real: 10
Percent real: 52.631578947
Count   real: 10
Percent real: 52.6315789473684
Percent real: 52.6

Obs    Name       Sex    Age    Height    Weight

  1    Alfred      M      14     69.0      112.5
  5    Henry       M      14     63.5      102.5
  6    James       M      12     57.3       83.0

Reading Real Numbers in hex16 from Macro Variables

%Macro PutNumber(ConversionType=
                ,Hex16 =
                );
%put Hex16:&Hex16.=%sysevalf(&Hex16.x);
%local I Number Type1 Type2 Type3 Type4 Type5 Dim_Type;
%Let Type1 =;
%Let Type2 =boolean;
%Let Type3 =ceil   ;
%Let Type4 =floor  ;
%Let Type5 =integer;
%Let dim_Type=5;
%do I = 2 %to &Dim_Type.;
    %let Number = %sysevalf(&Hex16.x,&&Type&I.);
    %put Number:&Number. : &&Type&I.;
    %end;
Data   _Null_;
attrib Number length = 8;
Number = input("&Hex16.",hex16.);
put Number= hex16. ' :: ' Number 32.15;
stop;
run;
%Mend;
%Put %sysfunc(putn(1234.56,hex16.));
%PutNumber(Hex16 = 40934A3D70A3D70A);
%Put %sysfunc(putn(-1234.56,hex16.));
%PutNumber(Hex16 = C0934A3D70A3D70A);
%PutNumber(Hex16 = 0000000000000000);
%PutNumber(Hex16 = 1111111111111111);
%PutNumber(Hex16 = 3FF0000000000000);
%PutNumber(Hex16 = 4000000000000000);
%PutNumber(Hex16 = 4010000000000000);
%PutNumber(Hex16 = 4020000000000000);
%PutNumber(Hex16 = 4030000000000000);
%PutNumber(Hex16 = 4040000000000000);
%PutNumber(Hex16 = 4050000000000000);
%PutNumber(Hex16 = 4060000000000000);

References

--Ronald_J._Fehd macro.maven == the radical programmer 17:20, 23 June 2012 (EDT) created: 25 August 2008