Data Acquisition in SAS

I am trying to change my data, but I have a difficult time. The data that I have looks something like this:

date abc ==================== 1996 5 7 8 1997 4 2 3 1998 1 9 6 

I want to modify the data (presumably using arrays) to get this:

 date val var ============= 1996 5 a 1997 4 a 1998 1 a 1996 7 b 1997 2 b 1998 9 b 1996 8 c 1997 3 c 1997 6 c 

So, I essentially put the variables (a, b, c) together with the corresponding date and variable name.

Thanks in advance!

+5
source share
2 answers

Use PROC TRANSPOSE to rotate data.

First sort by DATE

 proc sort data=have; by date; run; 

Then use transpose

 proc transpose data=have out=want(rename=(COL1=VAL _NAME_=VAR)); by date; var abc; run; 

Finally, it looks like you want this sorted by VAR and then DATE

 proc sort data=want; by VAR date; run; 
+6
source

Since you mention arrays, here is how you could achieve a result using them.
However, I would use the proc transpose method in the answer from @DomPazz, since the procedures are usually easier to read and understand by others who may need to view the code

 /* create initial dataset */ data have; input date abc; datalines; 1996 5 7 8 1997 4 2 3 1998 1 9 6 ; run; /* transpose data */ data want; set have; array vars{*} abc; /* create array of required values */ length val 8 var $8; /* set lengths of new variables */ do i = 1 to dim(vars); /* loop through each element of the array */ val = vars{i}; /* set val to be current array value */ var = vname(vars{i}); /* set var to be name of current array variable name */ drop abci; /* drop variables not required */ output; /* output each value to a new row */ end; run; /* sort data in required order */ proc sort data=want; by var date; run; 
+2
source

All Articles