BookmarkSubscribeRSS Feed
akhilesh_joshi
Fluorite | Level 6

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ballardw
Super User

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.

akhilesh_joshi
Fluorite | Level 6

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)

 

 

 

 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1637 views
  • 0 likes
  • 4 in conversation