Create a new variable from three other variables

Reply
Occasional Contributor
Posts: 8

Create a new variable from three other variables

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!

PROC Star
Posts: 169

Re: Create a new variable from three other variables

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; 
Occasional Contributor
Posts: 8

Re: Create a new variable from three other variables

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.

Super User
Posts: 8,590

Re: Create a new variable from three other variables

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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,005

Re: Create a new variable from three other variables

[ Edited ]

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;

Super User
Posts: 10,323

Re: Create a new variable from three other variables

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;
Occasional Contributor
Posts: 8

Re: Create a new variable from three other variables

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!

Ask a Question
Discussion stats
  • 6 replies
  • 107 views
  • 0 likes
  • 5 in conversation