vendredi 16 mars 2018

Counting the ID and assigning a Year

I have a dataset that looks like this:

data have;
input ID P1 P2 P3 P4;
datalines;

ID    P1    P2    P3    P4
12    10    15    20    30
12    -     20    5     3
12    -     -     25    33
12    -     -     -     30
19    10    15    20    30
19    -     10    17    30
19    -     -     5     30
19    -     -     -     30
;
run;

I am trying to build in a variable called Year which then can be used to identify that the ID and P1-P4 is an array with each row representing a year. Such that the dataset would look like.

data want;
set have;
input ID P1 P2 P3 P4;
datalines;

ID    P1    P2    P3    P4 Year
12    10    15    20    30 2017
12    -     20    5     3  2018
12    -     -     25    33 2019
12    -     -     -     30 2020
19    10    15    20    30 2017
19    -     10    17    30 2018
19    -     -     5     30 2019
19    -     -     -     30 2020
;
run;

I originally used to use this code:

Data Year;
    do ID = 1 to 8;
        do Year = 2017 to 2020;
        output;
        end;
    end;
run;

data Final;
set have;
Merge Year;
run;

But now that I am working with a different dataset each time and I don't know the structure of the ID, I can't keep changing ID=1 to 8 to suit the dataset each time.

My question: Is there a way to do this through the dataset, possibly a count?

Count ID = 2017;
Year = count + 1;

Aucun commentaire:

Enregistrer un commentaire