BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
derrick
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

1 REPLY 1
art297
Opal | Level 21

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;

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

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
  • 1 reply
  • 1145 views
  • 0 likes
  • 2 in conversation