BookmarkSubscribeRSS Feed
duel
Calcite | Level 5

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!

6 REPLIES 6
s_lassen
Meteorite | Level 14

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; 
duel
Calcite | Level 5

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.

Kurt_Bremser
Super User

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
Astounding
PROC Star

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;

Ksharp
Super User
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;
duel
Calcite | Level 5

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 418 views
  • 0 likes
  • 5 in conversation