DATA Step, Macro, Functions and more

How to print values between two particular integers in SAS using Do, Do-While loop

Reply
Contributor
Posts: 40

How to print values between two particular integers in SAS using Do, Do-While loop

Hi,

 

I am new to SAS programming. I am finding way to solve the following problem. 

The existing program prints 2 to 15 levels output by default. If I would like to print only the levels between a particular value (say 8) and 15, how do I achieve it using do or do-while loop.

Current sample output:

 

x_2 = x_1+(((1)/15)*y;
x_3 =x_1+(((2)/15)*y);
.
.

.
x_15 = x_1+(((14)/15)*y);

Trusted Advisor
Posts: 1,386

Re: How to print values between two particular integers in SAS using Do, Do-While loop

try next code, assuming you have X_1 and Y variables in a dataset::

 

%LET start = 8;

%LET stop = 15;

 

data want;

   set have;  /* input of X_1 and Y variables */

      length x_&start - x_&stop 8;

      array x_n x_&start - x_&stop ;

 

      do i=&start to &stop;

           j = i - &start +1;

          x_n(j) = x_1 + (i/15)*Y;   /* no need of all other brackets */

      end;

run;

proc print data=want; 

       var  x_1 y x_&start - x_&stop;

run;

Super User
Super User
Posts: 7,407

Re: How to print values between two particular integers in SAS using Do, Do-While loop

Please examine the instructions for posting questions.  Post example test data, in the form of a datastep.  And what you want the output to look like.  Also describe any formula to achieve the output.  I am afraid the given text does not describe the problem in any detail neede to give you an answer.

Super User
Posts: 10,511

Re: How to print values between two particular integers in SAS using Do, Do-While loop

[ Edited ]

It kind of helps to have some example input values and the actual desired output. And since you already have a program the entire program or at least the data step the example code came from. Since you do not show a PUT statement then you should clarify what you mean by "print" as there does not appear to be any "print" type output.

 

How are you telling the program that you want 8 to 15? Hard code in the program, based on the value of other variables in the dataset or something you provide interactively?

 

Also on this forum please do not post duplicate posts. If the post belongs in another topic area then you may get a suggestion to post the (possibly improved) question in an appropriate area or sometimes the post will be merged.

 

Duplicate posts end up with confusion as you get questions about your requirement that are answered in one thread but not the other and you feel "but I already answered that". Also others searching the forum may not find the thread with the accepted solution in the future.

Contributor
Posts: 40

Re: How to print values between two particular integers in SAS using Do, Do-While loop

Hi Ballardw,

 

Thank you for your reply. 

 

I wil try to be more concise and would avoid duplicate posts.

 

I already have x_1 and x_16. Currently, the program creates columns x_2 to x_15 in output data created after I run the program. Instead, I want to declare a variable say x_(number) where I can input a number of my choice say (10) to have columns only from that number(x_10) to x_16. y is X_16 - X_1.

 

so instead of x_1 I want to output to start from x_(number) so that x_(number) is my new x_1.

 

I create x_2 to x_15 currently by using 

 

x_2 = x_1 + ((1)/15)*y);

x_3 = x_1 + ((2/15)*y);

.

.

x_15 = x_1 + ((14/15)*y);

 

I include them in output data using 

DATA = name_of_data (KEEP = x_1 x_16 x_2 x_3 x_4 x_5 x_6 x_7 x_8 x_9 x_10 x_11 x_12 x_13 x_14 x_15)

 

 

 

 

 

 

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