Hello! I'm trying to transpose data using a DATA step and I'm currently stuck.
This is the data that I have:
Name | Var1 | Var2 | Var3 | Var4 |
Name 1 | 1 | 1 | 1 | 1 |
Name 2 | 2 | 2 | 2 | 2 |
Name 3 | 3 | 3 | 3 | 3 |
Name 4 | 4 | 4 | 4 | 4 |
Here is what I need my output data set to look like:
Name | Var1 | Var2 | Var3 |
Name 1 | 1 | 1 | 1 |
Name 1 | 2 | 2 | 2 |
Name 1 | 3 | 3 | 3 |
Name 2 | 4 | 4 | 4 |
Name 2 | 5 | 5 | 5 |
Name 2 | 6 | 6 | 6 |
Name 3 | 7 | 7 | 7 |
... | ... | ... | ... |
Here is the code I have so far:
DATA WORK.data;
INFILE DATALINES DLM = "," DSD;
INPUT Name $
Var1
Var2
Var3
Var4;
DATALINES;
Name1,1,2,3,4
Name2,1,2,3,4
Name3,1,2,3,4
Name4,1,2,3,4
;
DATA WORK.data2;
SET WORK.data;
KEEP Name Var1 Var2 Var3 Var4;
ARRAY Names (4) $5 Name1 Name2 Name3 Name4;
DO i = 1 TO DIM(Names);
Age = Names(i);
OUTPUT WORK.data2;
END;
RUN;
Here is the output data set I'm currently getting:
Name | Var1 | Var2 | Var3 | Var4 |
Name1 | . | 51 | 19 | 22 |
Name1 | . | 51 | 19 | 22 |
Name1 | . | 51 | 19 | 22 |
Name1 | . | 51 | 19 | 22 |
... | ... | ... | ... | ... |
Any help would be greatly appreciated!
data want;
set WORK.SwimMinutes;
array days[*] Mon Tue Wed;
do _n_ = 1 to dim(days);
day= _n_;
Minutes=days[_n_];
output;
end;
drop Mon Tue Wed;
run;
Since you want to manipulate days of the week why to you 1) attempt to use values of name as a loop and 2) attempt to assign character values (the names) to an age?
Apparently the other 3 classmates that asked this exact same question have deleted their questions or I could point to other posts on the forum.
I used the Name and Age variables to transpose the same data using PROC TRANSPOSE, so I figured using those variables would lead me to the same output.
data want;
set WORK.SwimMinutes;
array days[*] Mon Tue Wed;
do _n_ = 1 to dim(days);
day= _n_;
Minutes=days[_n_];
output;
end;
drop Mon Tue Wed;
run;
That worked perfectly! Thank you!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.