Desktop productivity for business analysts and programmers

SAS macro variable

Reply
Occasional Contributor
Posts: 9

SAS macro variable

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..??

Trusted Advisor
Posts: 1,613

Re: SAS macro variable

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.

Super User
Posts: 6,936

Re: SAS macro variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,401

Re: SAS macro variable

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.

Super User
Super User
Posts: 6,500

Re: SAS macro variable

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.));
Occasional Contributor
Posts: 9

Re: SAS macro variable

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. Smiley Happy

Super User
Posts: 6,936

Re: SAS macro variable


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. Smiley Happy


Please post your code and some example data (like:

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

).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: SAS macro variable

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 ;


 

Super User
Posts: 6,936

Re: SAS macro variable

Please test your example code yourself before posting it here.

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

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: SAS macro variable

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 ;
Super User
Posts: 6,936

Re: SAS macro variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: SAS macro variable

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..??

Super User
Posts: 6,936

Re: SAS macro variable


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)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: SAS macro variable

Is there a way I can send my DataSet.

 

I am using SAS EG 9.4.

 

Super User
Posts: 6,936

Re: SAS macro variable


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).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 21 replies
  • 290 views
  • 2 likes
  • 5 in conversation