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

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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