DATA Step, Macro, Functions and more

Extract Nondelemeted Values

Reply
Contributor JAR
Contributor
Posts: 45

Extract Nondelemeted Values

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

PROC Star
Posts: 7,363

Extract Nondelemeted Values

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;

Contributor JAR
Contributor
Posts: 45

Re: Extract Nondelemeted Values

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

PROC Star
Posts: 7,363

Re: Extract Nondelemeted Values

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;

PROC Star
Posts: 7,363

Re: Extract Nondelemeted Values

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;

Ask a Question
Discussion stats
  • 4 replies
  • 163 views
  • 0 likes
  • 2 in conversation