Hi, I have a problem. I have the following data
animal ID laktnr year zkz1_2 zkz2_3 zkz3_4 zkz4_5
100 1 2015 400
100 1 2015 400
100 3 2016 430
100 3 2016 430
100 4 2017 390
101 2 2015 405
101 2 2015 405
101 4 2017 500
200 1 2015 390
200 1 2015 390
200 1 2015 390
200 3 2017 420
I want to create a new variable 'zkz' that shows the zkz for every animal just once per year.
If laktnr is 1 then it should write the content from zkz1_2 into new variable zkz.
If laktnr is 3 then it should write the content from zkz3_4 into zkz.
animal ID laktnr year zkz1_2 zkz2_3 zkz3_4 zkz4_5 zkz(new variable)
100 1 2015 400 400
100 1 2015 400
100 3 2016 430 430
100 3 2016 430
100 4 2017 390 390
101 2 2015 405 450
101 2 2015 405
101 4 2017 500 500
200 1 2015 390 390
200 1 2015 390
200 1 2015 390
200 3 2017 420 420
I have no idea to solve the problem. Hope someone knows how to do it!
Thank you!
I think an array approach will work:
data want;
set have;
array arr (*) zkz1_2 zkz2_3 zkz3_4 zkz4_5;
zkz=arr(laktnr);
run;
I tried the array. Still not working. It just gives me zkz for first animal ID but I need for the first animal ID in a year the zkz. and if laktnr 1 is should take from zkz1_2 to zkz.
Try this:
data have;
infile cards dlm=',' dsd truncover;
input animal_ID laktnr year zkz1_2 zkz2_3 zkz3_4 zkz4_5;
cards;
100,1,2015,400
100,1,2015,400
100,3,2016,,,430
100,3,2016,,,430
100,4,2017,,,,390
101,2,2015,,405
101,2,2015,,405
101,4,2017,,,,500
200,1,2015,390
200,1,2015,390
200,1,2015,390
200,3,2017,,,420
;
run;
data want;
set have;
by animal_id year;
array arr {*} zkz1_2 zkz2_3 zkz3_4 zkz4_5;
if first.year then zkz = arr{laktnr};
run;
proc print data=want noobs;
run;
Result:
animal_ ID laktnr year zkz1_2 zkz2_3 zkz3_4 zkz4_5 zkz 100 1 2015 400 . . . 400 100 1 2015 400 . . . . 100 3 2016 . . 430 . 430 100 3 2016 . . 430 . . 100 4 2017 . . . 390 390 101 2 2015 . 405 . . 405 101 2 2015 . 405 . . . 101 4 2017 . . . 500 500 200 1 2015 390 . . . 390 200 1 2015 390 . . . . 200 1 2015 390 . . . . 200 3 2017 . . 420 . 420
This logic requires that the pattern remains in place. For example, if one observation contains a value for zkz1_2, then all observations for that animal/year contain that same value.
data want;
set have;
by animal year;
if first.year then zkz = ... use the logic with arrays already provided;
run;
data have;
infile cards truncover;
input animalID laktnr year zkz1_2 zkz2_3 zkz3_4 zkz4_5 ;
cards;
100 1 2015 400
100 1 2015 400
100 3 2016 . . 430
100 3 2016 . . 430
100 4 2017 . . . 390
101 2 2015 . 405
101 2 2015 . 405
101 4 2017 . . . 500
200 1 2015 390
200 1 2015 390
200 1 2015 390
200 3 2017 . . 420
;
run;
data want;
set have;
by animalID laktnr;
if first.laktnr then zkz=vvaluex(cats('zkz',laktnr,'_',laktnr+1));
run;
Thank you all!
I have now the solution!
I used:
data want;
set have;
by tiernummer jahr;
if first.jahr then zkz = max(zkz_lakt1_2_neu, zkz_lakt2_3_neu, zkz_lakt3_4_neu, zkz_lakt4_5_neu, zkz_lakt5_6_neu, zkz_lakt6_7_neu, zkz_lakt7_8_neu, zkz_lakt8_9_neu);
run;
Worked well!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.