DATA Step, Macro, Functions and more

CATX for use in temporary array, loss of precision

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

CATX for use in temporary array, loss of precision

I'm running SAS ver 9.2.

My goal is to obtain a string of numbers delimited by " " into a macro variable in order to use them as initial values for a _TEMPORARY_ array in a DATA STEP.  My method works but during the conversion I'm losing too many decimal places.

First I import the data from a CSV file with PROC IMPORT, which gives me columns with FORMAT BEST12.  Just to be safe I convert them to BEST32.  The numeric data is stored in the CSV file to about 16-18 decimal places (it's from R output).  The dataset is named dissert.lagalpha1 and consists of variables nodes1--nodes20 in BEST32. format.  I want a macro variables for all 20 nodes, seperated by spaces, with as much precision as possible.  I accomplish this with a DATA _NULL_ step and SYMPUTX function.

DATA _NULL_;

SET dissert.lag1alpha1(WHERE=(var1="&num_nodes"));

CALL SYMPUTX('nodes1',CATX(" ",of node1--node&num_nodes));

RUN;

The macro variable &num_nodes is 20.

The result is a list of numbers in BEST12. format, not BEST32, delimited by a blank space.  I can't figure out how to get CATX to maintain the BEST32. format of the variabes node1--node20.  can anybody think of another way to do this? 

I later use this variable in a _TEMPORARY_ array to decrease file size, because I'm essentially using the same 20 nodes over all 500 records in another dataset to perform a calculation.  I thought this way would be faster than just JOINING them.  But right now I think that might be the only viable option if I want to maintain the precision.

ARRAY arrNodes1 {&num_nodes} _TEMPORARY_ (&nodes1);


Accepted Solutions
Solution
‎11-27-2011 06:48 PM
PROC Star
Posts: 7,492

CATX for use in temporary array, loss of precision

There is probably a more efficient way to code this, and you are still going to be hit with a numeric precision issue (on windows it will only take up to 14 decimal places), but I think that the following will do what you want:

DATA _NULL_;

  SET dissert.lag1alpha1(WHERE=(var1=&num_nodes));

  array nodes(*) node:;

  length want $500.;

  want=put(nodes(1),best32.);

  do i=2 to &num_nodes.;

  want=catx(" ",want,put(nodes(i),best32.));

  end;

  CALL SYMPUTX('nodes1',want);

RUN;

View solution in original post


All Replies
Solution
‎11-27-2011 06:48 PM
PROC Star
Posts: 7,492

CATX for use in temporary array, loss of precision

There is probably a more efficient way to code this, and you are still going to be hit with a numeric precision issue (on windows it will only take up to 14 decimal places), but I think that the following will do what you want:

DATA _NULL_;

  SET dissert.lag1alpha1(WHERE=(var1=&num_nodes));

  array nodes(*) node:;

  length want $500.;

  want=put(nodes(1),best32.);

  do i=2 to &num_nodes.;

  want=catx(" ",want,put(nodes(i),best32.));

  end;

  CALL SYMPUTX('nodes1',want);

RUN;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 451 views
  • 0 likes
  • 2 in conversation