BookmarkSubscribeRSS Feed
Karina77
Calcite | Level 5

Please help me create 5 new variables ToTPass1 ~ ToTPass5 by using RETAIN and ARRAY statements to generate cumulative values in variables Pass1 to Pass5, respectively.

data psych;

  input ID $3. Ques1-Ques10 Score1-Score5;

  array S

  • Score1 - Score5;
  •     array F[5] _temporary_ (65, 70, 60, 62, 68);

        array P

  • Pass1 - Pass5;
  •     do i = 1 to dim(S);

        P = (S >= F);

        end;

        output;

    datalines;

    001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

    002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

    003 . . . . 5 5 4 4 3 3 68 69 80 75 70

    004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

    005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

    006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

    007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

    008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

    009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

    010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

    ;

    proc print data = psych;

    run;

    5 REPLIES 5
    KachiM
    Rhodochrosite | Level 12

    Again, the question is not understood. I guess you want the (0,1) are to be added in each ROW. If so, it is simple. If you want both Pass1 - Pass5 along with TotPass1 - TotPass5 then use the following:

    data psych;

      input ID $3. Ques1-Ques10 Score1-Score5;

      array S

  • Score1 - Score5;
  •     array F[5] _temporary_ (65, 70, 60, 62, 68);

        array P

  • Pass1 - Pass5;
  •      array T

  • TotPass1 - TotPass5;
  •     do i = 1 to dim(S);

        P = (S >= F);

         T + (S >= F);

        end;

        output;

    datalines;

    001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

    002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

    003 . . . . 5 5 4 4 3 3 68 69 80 75 70

    004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

    005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

    006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

    007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

    008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

    009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

    010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

    ;

    run;

    if you want only, TotPass1 - TotPass5, you may delete the statement,

    P = (S >= F);

    Karina77
    Calcite | Level 5

    I;m sorry my question was not clear. 

    What I need to do in this new question is to create 5 new variables: ToTPass1 ~ ToTPass5, for this of course I will use the ARRAY statement.  In each variable, I need the CUMULATIVE sum of the previous values (example,  observation n has Pass1 = 1, Pass2 = 0 , Pass3 = 0, Pass4 = 1, Pass5 = 1, then this observation  will have TotPass1 = 1, TotPass2 = 1, TotPass3 = 1, TotPass4 = 2, TotPass5 = 3.).   For this step, I will need to use the RETAIN statement.

    Please help.

    Thank you

    KachiM
    Rhodochrosite | Level 12

    I suppose this is what you want. I don't any need to use RETAIN as array p[ ] holds the values for the previous row. Only

    check is it should be done from the second row.

    data psych;

      input ID $3. Ques1-Ques10 Score1-Score5;

      array S

  • Score1 - Score5;
  •     array F[5] _temporary_ (65, 70, 60, 62, 68);

        array P

  • Pass1 - Pass5;
  •      array T

  • TotPass1 - TotPass5;
  •     do i = 1 to dim(S);

        P = (S >= F);

        if i > 1 then

         T + P;

        end;

        output;

    datalines;

    001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

    002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

    003 . . . . 5 5 4 4 3 3 68 69 80 75 70

    004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

    005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

    006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

    007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

    008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

    009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

    010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

    ;

    run;

    proc print; run;

    KachiM
    Rhodochrosite | Level 12

    I missed to compute the column totals in my previous Post. I am posting the revised code. This requires RETAIN statement.

    data Need(drop = i);

      input ID $3. Ques1-Ques10 Score1-Score5;

      array S

  • Score1 - Score5;
  •   array F[5] _temporary_ (65, 70, 60, 62, 68);

      array P

  • Pass1 - Pass5;
  •   array T

  • TotPass1 - TotPass5;
  •   retain TotP:;

       do i = 1 to dim(S);

          P = (S >= F);

          T + P;

        end;

        output;

    datalines;

    001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

    002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

    003 . . . . 5 5 4 4 3 3 68 69 80 75 70

    004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

    005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

    006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

    007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

    008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

    009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

    010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

    ;

    run;

    Ksharp
    Super User

    Code: Program

    data psych;
      input ID $3. Ques1-Ques10 Score1-Score5;
      array S[*] Score1 - Score5;
       array F[5] _temporary_ (65, 70, 60, 62, 68);
       array P[*] Pass1 - Pass5;
       sum=0;
       do i = 1 to dim(S);
       sum+(S[i] >= F[i]);
       P[i] =sum;
       end;  
       output;
    datalines;
    001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90
    002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71
    003 . . . . 5 5 4 4 3 3 68 69 80 75 70
    004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67
    005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99
    006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91
    007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82
    008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55
    009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88
    010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70
    ;
    run;

    sas-innovate-2026-white.png



    April 27 – 30 | Gaylord Texan | Grapevine, Texas

    Registration is open

    Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
    Register now and lock in 2025 pricing—just $495!

    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.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

    Discussion stats
    • 5 replies
    • 2099 views
    • 6 likes
    • 3 in conversation