BookmarkSubscribeRSS Feed
JAR
Obsidian | Level 7 JAR
Obsidian | Level 7

How do I read individual numeric values of 31415?

This is how my dataset to look like:

Digits

3

1

4

1

5

Regards,

JAR

4 REPLIES 4
art297
Opal | Level 21

I think your question may have gotten lost in its presentation.  Are you asking how one would do the following:

data have;

  input (x1-x5) (1.);

  cards;

31415

;

run;

JAR
Obsidian | Level 7 JAR
Obsidian | Level 7

Say, I need value of pi. This one-variable dataset contains only digits of pi.

However, the raw data contains value of pi as a string 314159265.... How do I do it?

Regards,

JAR

art297
Opal | Level 21

There are a number of ways.  One, for example, might be to use code like:

data have;

  input x;

  cards;

3

1

4

1

5

9

2

6

5

;

run;

data want;

  set have end=last;

  retain hold;

  if _n_ eq 1 then hold=catt(x,'.');

  else hold=catt(hold,x);

  if last then do;

    pi=input(hold,best12.);

    output;

  end;

run;

Of course, since there wasn't a decimal place in your example data, I had to force one in as part of the code.  However, if all you wanted was the value of pi, the following would be a much easier way to go:

data easier;

  pi=constant('pi');

run;

art297
Opal | Level 21

Just in case I have still misinterpreted what you asked, if you do have a one variable, one record dataset, with one string, how about?:

data have;

  input x $9.;

  cards;

314159265

;

run;

data want;

  set have end=last;

  pi=input(x,best12.8);

   /* or if you want it as a macro variable

     for use in other datasteps */

  call symput('pi',pi);

run;

 

data test;

  input x;

  x_times_pi=x*&pi.;

 

/* or using the bulit-in constant */

  x_times_pi_using_constant=x*constant('pi');

  cards;

2

3

;

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
  • 4 replies
  • 766 views
  • 0 likes
  • 2 in conversation