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);
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;
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.