Help using Base SAS procedures

Collapsing Observations using Arrays

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Collapsing Observations using Arrays

Hi,

The following is the code for converting multiple rows of info into single observation using ARRAYS..

Could someone explain to me the highlighted portions....

on page 15-16 of the following paper

http://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf

Thanks

proc sort data=expand;

by patient_ID date_of_visit;

run;

data collapse;

set expand;

by patient_ID;

/* Define an array for the new visit variables. */

array visit[4];

/* Retain the variables that are associated with the array. */

retain visit;

/* Clear the visit variables and the counter for each new BY */

/* group (Patient_ID). */

if first.patient_ID then call missing(of visit

  • , counter);         /*where is this "counter" variable coming from?there was no mention earlier. and what does call missing(of visit
  • ,counter mean
  • /* Increment the counter that is used to reference the element */

    /* of array to assign date. */

    counter + 1;                                                                 /*

    /* Assign the date to the proper element of the array. */

    visit[counter] = date_of_visit;                                       /*what happens when we use the array name followed by counter variable in braces assigned to a new variable?????*/

    /* Output one observation per BY group (Patient_ID). */

    if last.patient_ID then output;

    /* Format and drop variables, as desired. */

    format visit: mmddyy10.;

    drop date_of_visit counter;

    run;

    proc print;

    run;


    Accepted Solutions
    Solution
    ‎04-07-2013 08:57 PM
    Respected Advisor
    Posts: 4,640

    Re: Collapsing Observations using Arrays

    Array visit has 4 elements. You access an element of array visit with the bracket notation :

    x = visit[2];

    visit[3] = y;

    the first statement assigns the second element of array visit to x. the second statement assigns y to the third element of array visit.

    The statement call missing (of visit

  • , counter) assigns missing values to all the elements of array visit (the star notation means all elements) and to variable counter. It just so happens that this is the first time variable counter is mentioned.
  • hth

    PG

    PG

    View solution in original post


    All Replies
    Solution
    ‎04-07-2013 08:57 PM
    Respected Advisor
    Posts: 4,640

    Re: Collapsing Observations using Arrays

    Array visit has 4 elements. You access an element of array visit with the bracket notation :

    x = visit[2];

    visit[3] = y;

    the first statement assigns the second element of array visit to x. the second statement assigns y to the third element of array visit.

    The statement call missing (of visit

  • , counter) assigns missing values to all the elements of array visit (the star notation means all elements) and to variable counter. It just so happens that this is the first time variable counter is mentioned.
  • hth

    PG

    PG
    Super Contributor
    Posts: 1,040

    Re: Collapsing Observations using Arrays

    Thanks PG...

    Good luck on your renewal of SAS

    Super Contributor
    Posts: 1,040

    Re: Collapsing Observations using Arrays

    Hi ,

    I have some more questions regarding the sequence of the code ..

    1)If it is the first of the patient then we are telling to put as MISSING.....and then telling it to do Counter+1..........instead the dataset was already set by Patient ID/........So why cant we directly say Counter+1(instead of putting a dot for the first of the patient?????????

    Regards

    Respected Advisor
    Posts: 4,640

    Re: Collapsing Observations using Arrays

    For each patient, you want to start at counter = 1. The statement counter+1 sets the variable counter to 1, whether the previous value of counter was 0 or missing. - PG

    PG
    ☑ This topic is SOLVED.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 4 replies
    • 212 views
    • 3 likes
    • 2 in conversation