BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jporterf
Fluorite | Level 6

Hello! I'm trying to transpose data using a DATA step and I'm currently stuck. 

 

This is the data that I have:

NameVar1 Var2 Var3 Var4
Name 11111
Name 22222
Name 33333
Name 44444

 

Here is what I need my output data set to look like:

Name Var1 Var2 Var3
Name 1111
Name 1222
Name 1333
Name 2444
Name 2555
Name 2666
Name 3777
............

 

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.511922
Name1.511922
Name1.511922
Name1.511922
...............

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

jporterf
Fluorite | Level 6

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. 

r_behata
Barite | Level 11
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;
jporterf
Fluorite | Level 6

That worked perfectly! Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 900 views
  • 1 like
  • 3 in conversation