Loops in SAS

Looping is essential to statistical programming. Whether you need to iterate over parameters in an algorithm or indices in an array, a loop is often one of the first programming constructs that a beginning programmer learns.

Today is the first anniversary of this blog, which is named The DO Loop, so it seems appropriate to blog about DO loops in SAS. I'll describe looping in the SAS DATA step and compare it with looping in the SAS/IML language.

Loops in SAS

Loops are fundamental to programming because they enable you to repeat a computation for various values of parameters. Different languages use different keywords to define the iteration statement. The most well-known statement is the "for loop," which is used by C/C++, MATLAB, R, and other languages. Older languages, such as FORTRAN and SAS, call the iteration statement a "do loop," but it is exactly the same concept.

DO loops in the DATA step

The basic iterative DO statement in SAS has the syntax DO value = start TO stop. An END statement marks the end of the loop, as shown in the following example:

data A;
do i = 1 to 5;
   y = i**2; /* values are 1, 4, 9, 16, 25 */

By default, each iteration of a DO statement increments the value of the counter by 1, but you can use the BY option to increment the counter by other amounts, including non-integer amounts. For example, each iteration of the following DATA step increments the value i by 0.5:

data A;
do i = 1 to 5 by 0.5;
   y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.25, 25 */

You can also iterate "backwards" by using a negative value for the BY option: do i=5 to 1 by -0.5.

DO loops in SAS/IML Software

A basic iterative DO statement in the SAS/IML language has exactly the same syntax as in the DATA step, as shown in the following PROC IML statements:

proc iml;
x = 1:4; /* vector of values {1 2 3 4} */
do i = 1 to 5;
   z = sum(x##i); /* 10, 30, 100, 354, 1300 */

In the body of the loop, z is the sum of powers of the elements of x. During the ith iteration, the elements of x are raised to the ith power. As mentioned in the previous section, you can also use the BY option to increment the counter by non-unit values and by negative values.

Variations on the DO loop: DO WHILE and DO UNTIL

On occasion, you might want to stop iterating if a certain condition occurs. There are two ways to do this: you can use the WHILE clause to iterate as long as a certain condition holds, or you can use the UNTIL clause to iterate until a certain condition holds.

You can use the DO statement with a WHILE clause to iterate while a condition is true. The condition is checked before each iteration, which implies that you should intialize the stopping condition prior to the loop. The following statements extend the DATA step example and iterate as long as the value of y is less than 20. When i=4, the WHILE condition is not satisfied, so the loop iterates again.

data A;
y = 0;
do i = 1 to 5 by 0.5 while(y < 20);
   y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.5 */

You can use the iterative DO statement with an UNTIL clause to iterate until a condition becomes true. The UNTIL condition is evaluated at the end of the loop, so you do not have to initialize the condition prior to the loop. The following statements extend the PROC IML example. The iteration stops after the value of z exceeds 200.

proc iml;
x = 1:4;
do i = 1 to 5 until(z > 200);
   z = sum(x##i); /* 10, 30, 100, 354 */

In these examples, the iteration stopped because the WHILE or UNTIL condition was satisfied. If the condition is not satisfied when i=5 (the last value for the counter), the loop stops anyway. Consequently, the examples have two stopping conditions: a maximum number of iterations and the WHILE or UNTIL criterion. SAS also supports a DO WHILE and DO UNTIL syntax that does not involve using a counter variable.

It is worth noting that a DO loop with an UNTIL clause always executes at least one time because the condition is evaluated at the end of the loop. To prevent this behavior, use a DO loop with a WHILE clause.

Looping over a set of items (foreach)

Some languages support a "foreach loop" that iterates over objects in a collection. SAS doesn't support that syntax directly, but there is a variant of the DO loop in which you can iterate over values in a specified list. The syntax in the DATA step is to specify a list of values (numeric or character) after the equal sign. The following example iterates over a few terms in the Fibonacci sequence:

data A;
do v = 1, 1, 2, 3, 5, 8, 13, 21;
   y = v/lag(v);

The ratio of adjacent values in a Fibonacci sequence converges to the golden ratio, which is 1.61803399....

The SAS/IML language does not support this syntax, but does enable you to iterate over values that are contained in a vector (or matrix). The following statements create a vector, v, that contains the Fibonacci numbers. An ordinary DO loop is used to iterate over the elements of the vector. At the end of the loop, the vector z contains the same values as the variable Y that was computed in the DATA step.

proc iml;
v = {1, 1, 2, 3, 5, 8, 13, 21};
z = j(nrow(v),1,.); /* initialize ratio to missing values */
do i = 2 to nrow(v);
   z[i] = v[i]/v[i-1];

Avoid unnecessary loops in the SAS/IML Language

I have some advice on using DO loops in SAS/IML language: look carefully to determine if you really need a loop. The SAS/IML language is a matrix/vector language, so statements that operate on a few long vectors run much faster than equivalent statements that involve many scalar quantities. Experienced SAS/IML programmers rarely operate on each element of a vector. Rather, they manipulate the vector as a single quantity. For example, the previous SAS/IML loop can be eliminated:

proc iml;
v = {1, 1, 2, 3, 5, 8, 13, 21};
idx = 2:nrow(v);
z = v[idx]/v[idx-1];

This computation, which computes the nonmissing ratios, is more efficient than looping over elements. For other tips and techniques that make your SAS/IML programs more efficient, see my book Statistical Programming with SAS/IML Software.

tags: SAS Programming, Statistical Programming


  1. Gustavo Cano
    Posted September 9, 2011 at 1:37 pm | Permalink

    it have an algorithm likely the last one but for DO WHILE?.

    proc iml);
    v = {1, 1, 2, 3, 5, 8, 13, 21};
    idx = 2:nrow(v);
    z = v[idx]/v[idx-1];

    but for DO WHILE. For example i think using the LOC function.... Note: In the algorithm the ")" is wrong...


  2. oscar
    Posted May 14, 2012 at 5:36 pm | Permalink

    I have a small problem which I think can be solved by loops in SAS, but I don't know how, I appreciate if you help me with that:

    I have a data set in which if a value is missing then it shall be assigned the nearest non missing value in the previous observations, for example, 6 rows of my data looks like this:
    I want to assign the missing values so that it would read like this
    Can SAS do that for me?

    • Posted May 14, 2012 at 7:41 pm | Permalink

      Yes, of course. Direct questions like this to the SAS Community Forum on DATA step programming..

    • Luke Du
      Posted December 12, 2012 at 12:29 pm | Permalink

      @Oscar: Here is the solution to your question.
      *data set b4 with variable a as you have;
      data b4;
      data after;
      retain b;
      set b4;
      if not missing(a) then b=a;
      *data set after has variable b as you required;

  3. sirisha pannala
    Posted October 1, 2012 at 5:09 pm | Permalink

    In sas Using an array how to find the sum of higest two HWs from HW1--HW4

    • Posted October 1, 2012 at 5:44 pm | Permalink

      For help with SAS DATA step programming, post your questions to the SAS DATA step community.

      • sushma
        Posted April 5, 2013 at 10:39 am | Permalink

        sir, i have a question like cost of vehicle is 5000 and income every day is 100..how many days he will purchase the vehicle ..can we do this by using do until?

        • Posted April 5, 2013 at 10:47 am | Permalink

          Yes, you could do that. It might look like this:
          do day=1 to 1000 until(pay>=cost);

  4. Romulo Freitas
    Posted May 13, 2013 at 4:59 pm | Permalink

    How do I create a column sequential as counter lines in SAS programming?

    • Posted May 14, 2013 at 5:45 am | Permalink

      I don't understand what you are asking, but you can post questions and have discussions about SAS programming at the SAS Support Community.

    • cargoltreubanya
      Posted September 23, 2013 at 11:42 am | Permalink

      Try with:

      DATA b;
      SET a;

  5. sureshkumar
    Posted May 29, 2013 at 8:05 am | Permalink

    i have a question:
    data _null_;
    do until(whereishi=0);
    if whereishi=0 then put "The End";

    even though the do loop is satisfying intially with zero still it is looping,i could see in log The end .How come?

    • Posted May 29, 2013 at 8:18 am | Permalink

      As the article says, "the UNTIL condition is evaluated at the end of the loop." This implies that an UNTIL loop always executes at least once. In contrast, a WHILE loop will not execute if the condition is false.

  6. Apoorv Mishra
    Posted June 5, 2013 at 12:01 pm | Permalink

    Why this code is not working, Basically I am splitting the data, When I was trying to print one of them, its showing the data set is not created, could anyone help on the same.

    %DO i = 1 %TO 10;
    CREATE TABLE itemdef_&i AS
    SELECT var2
    FROM three
    WHERE unique_key = "itemdef.&i";

    PROC PRINT DATA = itemdef_1;

  7. Brian
    Posted June 13, 2013 at 2:48 pm | Permalink

    I have a column for each employee start date (startdt1 thru startdt12) in a new position, and a column for their last promotion date. How can I create a loop that will tell me when the promotion date is between startdt(i) and startdt(i+1) and output to a new variable with "Start_DT(i)" ? Thanks.

  8. Mark
    Posted October 10, 2013 at 1:09 pm | Permalink

    Perhaps this is not something the DO LOOP command does, but I am looking for an elegant way to repeat an analysis, though changing the outcome in each analysis. I'm using proc glimmix and need to model ~50 outcomes and would like to avoid the tedium of waiting for the run to complete, changing the outcome, running again... If the loop command doesn't do this, can anyone refer me to the command (or macro) that does (if there is one)? Thanks, Mark

    • Posted October 10, 2013 at 2:04 pm | Permalink

      1) It sounds like you want to use the BY statement, which loops over all values of a discrete variable such as US states or the numbers 1-50.
      2) If you are doing a simulation, you might want to read about the advantage of the BY statement over macro loops.
      3) If I haven't answered your question, post your question to the SAS Support Community

      • Mark
        Posted October 11, 2013 at 7:46 am | Permalink

        Hi Rick - Thank you for your prompt reply. I don't think the BY statement will do what I'm looking for: I need to run models across different outcome variables, not different values of one outcome variable. Thanks for the reference to the SAS Support Community; I'll try that next. Best, M

        • Posted October 11, 2013 at 8:06 am | Permalink

          OK, good luck. If the explanatory variables and form of the model are the same for each, you can use a macro %DO loop, but I still think the most efficient option is to concatenate the data set and use a BY statement. Instead of N obsevations that look like
          Y1 Y2 ... Y50 X1 X2 .. Xk
          you will stack the data and have 50*N observations that look like this:
          1 Y1 X1 X2 .. Xk
          2 Y2 X1 X2 .. Xk
          50 Y50 X1 X2 .. Xk

  9. Chung
    Posted November 7, 2013 at 9:15 pm | Permalink

    Rick I am wondering (1) can I use the do -loop when i have got n variables, lets say X1,X2,...,Xn, and i would like to introduce a new variable Z s.t. Z=X1+X2+X3+...+Xn
    (2) If there is any hidden(or obvious that I have not notice) problem when I delete the continue word and the whole leave statement in the following programme written:
    "data q1;
    i = 100;
    do while (1);
    i = i - 2;
    if mod(i,6)=0 then do;
    if mod(i,4)=0 then do;
    if mod(i,7)=0 and mod(i,5)=0 then leave;

    the thoughts are driving me crazy!

  10. nikhil
    Posted February 3, 2014 at 9:25 am | Permalink

    Hi Rick ,

    my query is i have table ABC in which one column name is table_name which contains n different tables names. I want to create a separate data sets for the corresponding table names. can you please help me with this.

    • Posted February 3, 2014 at 9:33 am | Permalink

      See my previous response. Go to the SAS Support Communities to get answers to questions like this.

  11. Amy
    Posted May 6, 2014 at 9:56 am | Permalink

    Hi Rick,

    I have a dataset with 3 columns, Customer ID, Product Key and Sales. Could you please advise how I can convert the table into something like the following:

    Customer ID, Key1 _Sales, Key2_Sales, Key3_Sales, and so on.

    Thank you for your help in advance.

  12. momoe
    Posted June 20, 2014 at 1:24 am | Permalink

    I am a new user in SAS. When I am using DO-LOOP in SAS, I get a hesitation with the use in SET dataset. Simply speaking, I would like to know the following difference in the result:

    **** input data source;
    data a;
    input name $1-10 test 12-15 score 17-22;

    Kitty 1 70
    David 1 80
    May 1 95
    David 3 .
    May 3 95
    David 2 85
    Kitty 3 .
    Kitty 2 90
    May 2 95
    proc sort; by name test;

    *e.g. 1 : set dataset outside do-loop, and 9 observations are included in data temp;
    data temp;
    set a;
    do x=1 to 10;

    *e.g. 2 : set dataset inside do-loop, and 1 observation (_N_=10) is included only in data temp;
    data temp;
    do x=1 to 10;
    set a;

    • momoe
      Posted June 20, 2014 at 1:25 am | Permalink

      May I thank those who can solve my weakness in SAS.

    • Posted June 20, 2014 at 6:32 am | Permalink

      That's a good question. Understanding these cases requires a discussion of the implicit loop in the DATA step, and how SET, DO loops, and the OUTPUT statement interact. I suggest you post this and future questions to the SAS Support Community.

  13. Dusty Simmons
    Posted July 9, 2014 at 10:18 am | Permalink

    I want to read a file sequentially, and then for each row on that file, using data from that row, perform an SQL proc to retrieve data associated with that data, to write out all of the data.
    IE: how do I loop through the SQL proc for each row on the input sequential file?

  14. Thokozani
    Posted October 9, 2014 at 7:19 am | Permalink

    Hi all,

    I have data for stocks (stock prices, returns, abnormal returns and cumulative abnormal returns) for the period 31 Jan 2007 and 31 Dec 2013. I have further given each date a stock Month Number (StockMonthNumber).

    So, I now want sas to given me the mean, median, t-stat and p-value for my CumulativeAbnormalReturns only for up to month 6. How do I do this?

  15. Thokozani
    Posted October 9, 2014 at 7:21 am | Permalink

    Do I say:

    Proc univariate date=work.thokozani;
    var CummulativeAbnormalReturns;
    Where StockMonthNumber<=6;

    • Posted October 9, 2014 at 9:04 am | Permalink

      You can ask questions like this at the SAS Support Communities.

      • George
        Posted May 28, 2015 at 6:48 pm | Permalink

        I am trying to calculate the following.

        price=lag(price)*cpi/100. I have hand-calculated the price for the year 1979: 102.6133=101.19x100.07/100. The next year (1980) should be 102.6133*107.5/100. What is the SAS algorithm?

        year cpi price
        1978 100.07 100.07
        1979 101.19 102.6133
        1980 107.5 ?

  16. bash
    Posted March 24, 2015 at 5:29 pm | Permalink

    I want to simulate a bivariate dataset (say weight vs blood pressure) with normal distribution, after which I would like to create a missing completely at random scenario with certain observations in the blood pressure variables missing. I would then like to perform mean imputation (or multiple imputation) for the missing observations to compare the mean and variance between the original (before missingness was induced) and the imputed dataset. I intend to do this multiple of times to get the average mean and variance for the imputed data, for the comparison with the original. I feel I could do this using SAS but can't figure out how, because I am a very basic user.
    Could any one help figure out how I could do that please?

  17. matthew Freeman
    Posted June 3, 2015 at 1:15 am | Permalink

    what is wrong with my macro code, I want to create 36 versions of gb flag


  18. Arijit Chaki
    Posted June 15, 2015 at 2:26 am | Permalink

    If i want to run the do loop such that instead of i=1 to 12 i want to make it i=jan to dec. Is it possible to do this. If yes then how? Cz i have tried a lot but not happening. Plz do rply sir.

    • Posted June 15, 2015 at 6:26 am | Permalink

      Yes, it is possible, but I would need more details to provide the code. Post your question to one of the SAS Support Communities, either the one for DATA step or the one for SAS/IML.

  19. Abdifatah
    Posted July 1, 2015 at 2:41 am | Permalink


    I am new to SAS and need help with properly using the DO loop. I am doing a simulation in SAS, and I'm using R to simulate the DATA and then bring it back to SAS to use specific functions.

    Here is my code. My problem is I am finding it difficult using the DO loop to reiterate over the code as many times I would like while saving specific parameters every time it goes through a single iteration. Any help with this would help. Thanks!

    data Meta_Results5;
    do i= 1 to 3;

    proc iml; /* using the SAS/IML environment to run the R code */
    submit / R;

  20. Jman
    Posted September 7, 2015 at 8:31 pm | Permalink

    My turn to ask a question:
    I need to write the line below for each year from 1997 to 2010:
    filename r1997 pipe ' R:\7za.exe e "R:\Data\registry1997.B.dat.gz" "registry1997.dat" -y -so '; data r1997; infile r1997; &i; run;

    Is there a way to write it only once in a loop? I really appreciate any help.

  21. Maja
    Posted September 23, 2015 at 3:00 pm | Permalink

    I often follow your advises on sas community. Here I see you are posting about do loops. Today I was playing with some data and found out a problem. I have 5 data sets in txt format named roh_1.txt, roh_2.txt, roh_4.txt, roh_8.txt and roh_16.txt
    I would like to make macro which will allow me to import those into SAS. I am familiar with infile option, but having issues with making macro. Of course I could rename files to be 1 to 5 but... Well I don't want to. Can you please give some hints.

    • Posted September 23, 2015 at 3:04 pm | Permalink

      Please post your question and sample code to the SAS Support Communities.

  22. hend gaber
    Posted November 9, 2015 at 5:13 pm | Permalink

    i have a question
    there are some missing in data , i calculate -((xi-xj)^2)^0.5 for x2&x3 (contain complete data)
    x1 x2 x3
    1 3 2
    2 1
    3 2 1
    4 1

    how i can write this on sas?

    • Posted November 10, 2015 at 6:49 am | Permalink

      It's not clear if you are asking about how to represent missing values or how to write the computation. The dot (.) is the missing value in SAS, so you can create the data set like this:

      data A;
      input x1 x2 x3;
       1 3 2
       . 2 1
       3 2 1
       . 4 1

      If you have a question about the computation, post it to a SAS Support Community

  23. Billy Bob
    Posted January 18, 2016 at 10:13 am | Permalink

    Rick ... you da man !!

  24. Posted February 2, 2016 at 3:00 am | Permalink

    I have a sas dataset as below.
    fix var1 var2 var3
    1 a b c
    2 p q r

    But I want to create a sas dataset as below
    1 a
    1 b
    1 c
    2 p
    2 q
    2 r
    Anybody can help me that to write a sas code

  25. Meredith
    Posted February 10, 2016 at 3:27 pm | Permalink

    Do you have a template for a do loop to do the following: I need to filter a large file by ID and then export the files into a folder, the files named with ID.

    • Posted February 10, 2016 at 3:43 pm | Permalink

      The basic template does not require a DO loop:

      data out1 out2 out3;
       set bigfile;
       if ID=1 then output out1;
       else if ID=2 then output out2;
       else if ID=3 then output out3;

      However, there are many reasons why you should NOT split data sets. For some of the reasons (and other techniques that split data sets), see "Split data into subsets."

  26. ove
    Posted March 1, 2016 at 2:04 am | Permalink

    data A;
    y = 0;
    do i = 1 to 5 by 0.5 while(y < 20);
    y = i**2; /* values are 1, 2.25, 4, 6.25, ..., 16 */

    but the output reads:
    y i
    1 1
    2,25 1,5
    4 2
    6,25 2,5
    9 3
    12,25 3,5
    16 4
    20,25 4,5

    • Posted March 1, 2016 at 8:55 am | Permalink

      Since the numbers are correct, I assume that you are confused that the Y variable is printed first and the i variable is second?
      The DATA step creates variables in the order that it sees them. The first statement sets y=0, so that that is why Y appears first in the data.
      You can use PROC PRINT to print the data in any order:
      proc print data=A; var i y; run;
      You can also delete the "y=0;" statement and the i variable will appear first.

      • ove
        Posted March 7, 2016 at 2:04 am | Permalink

        No, what might confuse is:
        /* values are 1, 2.25, 4, 6.25, ..., 16 */ in the comment.
        The output data set contains the value 20.25 as well.

        I see I used: , as comma instead of: . in my first comment.
        Maybe that confused you :).

  27. James Walker
    Posted July 8, 2016 at 1:27 pm | Permalink

    I use this type of code frequently for making changes in place on a large dataset in reverse, since the obs I'm normally updating are at the bottom and then I stop once the record has been found, but I can't get the code to work for just removing a record any thoughts?

    data CRRG.CRRG;
    do k = nobs to 1 by -1;
    modify CRRG.CRRG nobs= nobs point=k;
    if arrangement_key ='yyyyy' and as_of_date = '30Jun2016'd then do;
    CUST_NUM = 'xxxxxx';

    I tried

    data CRRG.CRRG;
    do k = nobs to 1 by -1;
    modify CRRG.CRRG nobs= nobs point=k;
    if arrangement_key ='yyyy' and as_of_date = '30Jun2016'd then remove;

  28. Jon Dickens
    Posted July 10, 2016 at 6:53 am | Permalink


    When using two or more Do Loops should they always be NESTED or can they OVERLAP?

    DO /* Start of Do Loop One */
    SAS Statements for Loop One ;

    DO /* Start of Do Loop Two */
    SAS Statements for Loop Two ;
    END ; /* End of Do Loop Two */

    More SAS Statements for Loop One ;
    END ; /* End of Do Loop One */

    DO /* Start of Do Loop One */
    SAS Statements for Loop One ;

    DO /* Start of Do Loop Two */
    SAS Statements for Loop Two ;

    SAS Statements for Loop One ;

    END ; /* End of Do Loop One */

    More SAS Statements for Loop Two ;

    END ; /* End of Do Loop Two */

    If it is legal to overlap Do Loop structures, when would you need to do so?


    • Posted July 10, 2016 at 3:01 pm | Permalink

      The END statement always matches the closest DO staetment, so loops are always nested and never "overlap." This same fact is true in all computer languages that I am familiar with.

  29. Hitesh
    Posted August 7, 2016 at 2:31 pm | Permalink

    How can I print my name 10 times or more using do loop

    • Posted August 7, 2016 at 5:27 pm | Permalink

      Thanks for writing. It sounds like you are beginning to learn SAS programming. Congratulations. Since you will probably have many questions, I recommend that you post your questions to the SAS Support Communities. You'll be an expert in no time!

  30. Nitika Dhingra
    Posted September 23, 2016 at 8:30 pm | Permalink

    I have to use the value of a formula calculated in one row and then use the answer to calculate the value for the next row.
    Foe eg: when the first instance is encountered it calculates the formalue and after that keeps re-using the pred value in the next few time - How can do it?
    data starting_point_3;
    set starting_point_2;
    by key;
    if firstKey then pred=(intercept+coeff_lag*pred_last_week);

  31. nulix
    Posted September 28, 2016 at 4:29 pm | Permalink

    Please i have ten tables created from
    %n=1 %to &nobs
    data test&n. ..........

    So now i want to have all tables in one table,
    I use proc append. it works but i some variables aren't found in the base.
    So can i think i can use proc iml and do loops to create matrix and concatenate horizontally.
    I don't know how proceed.
    Can you help me please?

    • Posted September 28, 2016 at 4:44 pm | Permalink

      Post your question, example data, and the SAS code you are attempting at the SAS Support Communities.

      • nulix
        Posted September 29, 2016 at 1:04 pm | Permalink

        %macro te();
        %do n=1 %to 10;
        data test&n. ( keep = id &idn. y1 yd yd1 - yd&p.);
        set mytable (where=(id=&n.)) nobs=nobs;
        array un {*} yd yd1 - yd&p.;
        do j=2 to dim (un);
        un {j}= lag(un{j-1});
        end;drop j;
        %macro te();

        I get 10 datasets but i don't know how proceed to concatenate without lose variables because with proc append some variables aren't added.


  32. Deepti
    Posted October 15, 2016 at 3:11 am | Permalink

    i have a problem to solve this
    A= {1 2 3 4 5}
    B= {1 2 1}

    step 1: A= {1 2 3 4 5}
    B= {1 2 1}
    step 2: A= {1 2 3 4 5}
    B= {1 2 1}
    step 3: A= {1 2 3 4 5}
    B= {1 2 1}

    please tell me the method & which loop i used to solve this.
    if you get that method kindly reply soon & after that tell me if the length of A & B is increasing means we dont know the length then which method should i use?

  33. Jay
    Posted October 30, 2016 at 6:39 am | Permalink

    Hello, Rick,

    I tried transforming the followings into a more efficient code using DO LOOP, but i failed. Can you please give me some guide for this?

    data Input1;
    set Input1;
    if no= 2 then no= 2 + 0.1;
    data Input2;
    set Input2;
    if no= 2 then no= 2 + 0.2;
    data Input3;
    set Input3;
    if no= 2 then no= 2 + 0.3;

    I switched the above to the below.

    DO i= 1 to 3;
    data Input&i;
    set Input&i;
    if no= 2 then no= 2 + 0.1 * &i;


  34. Keith Johnson
    Posted January 5, 2017 at 11:52 am | Permalink

    Hello I would like to do a do loop over a list selected from a proc sql small data set. (in this case a 103 obs)

    proc sql;
    create table TERM_TBL_DIST as
    select DISTINCT strm||'*'||acad_career as key, STRM, ACAD_CAREER
    from &snapshotlib..PS_TERM_TBL
    WHERE ACAD_YEAR BETWEEN "&year_four." and "&year.";
    proc print data = TERM_TBL_DIST noobs n;run;

    where my 'key' is the control and the data looks like:


    2153*UGRD 2153 UGRD
    2153*VETM 2153 VETM
    2155*BUSN 2155 BUSN
    2155*GRAD 2155 GRAD
    2155*IALC 2155 IALC
    2155*MEDI 2155 MEDI
    2155*PHAR 2155 PHAR
    2155*UGRD 2155 UGRD
    2155*VETM 2155 VETM
    2157*BUSN 2157 BUSN
    2157*GRAD 2157 GRAD
    2157*IALC 2157 IALC
    2157*MEDI 2157 MEDI
    2157*PHAR 2157 PHAR
    2157*UGRD 2157 UGRD
    2157*VETM 2157 VETM
    2160*UGRD 2160 UGRD
    2163*BUSN 2163 BUSN
    2163*GRAD 2163 GRAD
    2163*IALC 2163 IALC
    2163*MEDI 2163 MEDI
    2163*PHAR 2163 PHAR
    2163*UGRD 2163 UGRD
    2163*VETM 2163 VETM
    2165*BUSN 2165 BUSN
    2165*GRAD 2165 GRAD

    I notice a part in the above information that made it looks like any random txt can make up the list of control var, but can I auto populate this list or do I have to prerun my list and stuff the data into a written out list in my code? If this is possible can someone show me how to get my data in to this list? TIA. -KJ

One Trackback

  1. By Do Loops, Probabilities and Simulations | Analytics on September 28, 2015 at 10:12 pm

    […] Using Do Loops is extremely useful when it comes to statistical programming since it makes building your program a much simpler task. Looping still blows my mind every time I see it in action. It is important to notice that there is a difference between the keywords used in SAS and other programs like R. If you would like to read more about it, please click here […]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>