SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
guptagaurav12
Calcite | Level 5

I have a macro variable to store decimal values upto 10 places and then create a dataset with a column consisting of that values.

 

But I am facing a problem as the macro variable is not storing the value upto 10 decimal places and I can't find a way to give format or something to the macro Variable so that it can store the exact same value provided to it and then pass that value to the new dataset created.

 

how can I store values upto 10 decimal places in a macro variable..??

21 REPLIES 21
PaigeMiller
Diamond | Level 26

You need to be much more specific and give an example of your code and example showing the data, so we can see what you are doing and what is not working.

 

But ... I agree with the usual advice in this forum ... macro is primarily a text processor, it shouldn't normally be used for numbers ... so my guess is that there is a non-macro solution that will work better for you.

--
Paige Miller
Kurt_Bremser
Super User

You can store values up to 65534 characters in a macro variable, so the length is not your problem.

I guess that you are losing digits on the conversion to numeric because you are exceeding the numeric precision inherent in 64-bit real numbers (~15 decimal digits).

Please post some values that don't work, and the code you used.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Macro varibles are text and hence can contain anything.  Where is the data coming in from?  There are a number of methods of reading data into SAS, be it from text files, Excel, databases.  Macro variables however are not a data medium, but a method of find/replace code generation.  Perhaps show where you get this data from and handle it.

Tom
Super User Tom
Super User

Most likely you have let SAS convert the number to a character string to store into the macro variable and it has defaulted to using BEST12. format for the conversion.  Use your own format instead.

call symputx('mymvar',put(myvar,best32.));
guptagaurav12
Calcite | Level 5

i have a column named FIRST with a value 0.0328565628 and I am storing this in a macro variable xyz and then this value is stored in another column named SECOND but when I am creating a third column named THIRD which is equal to FIRST/SECOND then it is giving a value of 0.999999 but I am expecting this to be complete 1 because logically both the columns FIRST and SECOND are exact same.

 

I can't understand the reason for this. 

Hope someone can help. 🙂

Kurt_Bremser
Super User

@guptagaurav12 wrote:

i have a column named FIRST with a value 0.0328565628 and I am storing this in a macro variable xyz and then this value is stored in another column named SECOND but when I am creating a third column named THIRD which is equal to FIRST/SECOND then it is giving a value of 0.999999 but I am expecting this to be complete 1 because logically both the columns FIRST and SECOND are exact same.

 

I can't understand the reason for this. 

Hope someone can help. 🙂


Please post your code and some example data (like:

data have;
input first;
cards;
0.0328565628
;
run;

).

guptagaurav12
Calcite | Level 5
data have;
input first;
cards;0.0328565628;
run;Data have ;
set have ;
call symput('OBSERVEDAVGVAR',  ObservedAverages); 

run ;

Data Have2 ;
Set have ;

Second = &OBSERVEDAVGVAR ;

Run ;

Data have3 ;
Set have2 ;

Third = First/Second ;

Run ;


 

Kurt_Bremser
Super User

Please test your example code yourself before posting it here.

There is no variable ObservedAverages in dataset have, so the macrovar ends up empty.

 

guptagaurav12
Calcite | Level 5

Sorry My Mistake.

 

data have;
input first;
cards;0.0328565628;
run;Data have ;
set have ;
call symput('OBSERVEDAVGVAR',  First); 

run ;

Data Have2 ;
Set have ;

Second = &OBSERVEDAVGVAR ;

Run ;

Data have3 ;
Set have2 ;

Third = First/Second ;

Run ;
Kurt_Bremser
Super User

I see no problem here:

data have;
input first;
cards;
0.0328565628
;
run;

data _null_;
set have;
call symput('OBSERVEDAVGVAR',first);
run;

%put &OBSERVEDAVGVAR;

data have2;
set have;
second = &OBSERVEDAVGVAR;
run;

data have3;
set have2;
third = first / second;
format third best32.;
if third = 1 then flag = 'yes';
run;

proc print data=have3 noobs;
run;

Result:

  first      second                                third    flag

0.032857    0.032857                                   1    yes 

Not only does third display as 1, it is really equal to 1, as the conditional statement shows.

guptagaurav12
Calcite | Level 5

Sorry clicked on Accept as Solution by mistake.

 

Ya it should display one but it displays 1 for only first row and from second row onwards it display 0.99999.

 

can't understand why..??

Kurt_Bremser
Super User

@guptagaurav12 wrote:

Sorry clicked on Accept as Solution by mistake.

 

Ya it should display one but it displays 1 for only first row and from second row onwards it display 0.99999.

 

can't understand why..??


When there's different outcomes with the same code, then the reason is in the data. Please post the real values that cause the erratic behaviour.

If in doubt, use the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to convert your data to a data step for posting. This method will preserve values as they are (not as they are displayed)

guptagaurav12
Calcite | Level 5

Is there a way I can send my DataSet.

 

I am using SAS EG 9.4.

 

Kurt_Bremser
Super User

@guptagaurav12 wrote:

Is there a way I can send my DataSet.

 

I am using SAS EG 9.4.

 


Yes. Use the macro from the link I gave you. This is the proper method for posting example data that will preserve data and attributes, clear all firewalls, and not cause a problem because of incompatible SAS versions (DBCS vs. SBCS vs. UTF-8, little/big-endian).

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 21 replies
  • 4124 views
  • 2 likes
  • 5 in conversation