I have a set of SAS data in which each individual entry needs to be reassigned to a temporary dataset with only 2 variables: its color and the number. Right now the set is organized into columns like this:
Black | Gray | Red | White |
---|---|---|---|
57 | 59 | 60 | 78 |
67 | 76 | 81 | 50 |
52 | 62 | 47 | 66 |
EDIT: The code I had set up was something like this, and I changed it with suggestion from answers on here.
data Dataset1new;
set Dataset1;
input Color $ Number;
do i = 1 to *# of array rows*;
do j = 1 to *# of array columns*;
Number = Dataset1[i + j - 1];
Color = vname(Dataset1[i + j - 1]);
run;
I originally thought about using a do statement to iterate over each entry, but am having difficulty setting it up.
Use an array and loop over every value. Use the name of the variable to identify its color.
data want;
set have;
array c[*] Black--White;
do i = 1 to dim(c);
color = vname(c[i]);
value = c[i];
output;
end;
keep color value;
run;
This is a more generalized way compared to stacking the data four times:
data want;
set have(in=b rename=(black=value))
have(in=g rename=(gray=value))
have(in=r rename=(red=value))
have(in=w rename=(white=value))
;
select;
when(b) color='Black';
when(g) color='Gray';
when(r) color='Red';
when(w) color='White';
otherwise;
end;
keep color value;
run;
color value
Black 57
Gray 59
Red 60
White 78
Black 67
Gray 76
Red 81
White 50
Black 52
Gray 62
Red 47
White 66