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

I have the first 2 rows of data which looks as given (Q1-Q9 are the variable names)

 

Q1Q2Q3Q4 Q5 Q6 Q7Q8Q9
Colour   Shape   Children   
red blue greensquarecircle 1234

 

I would like the rows to look as given below

 

Q1Q2Q3Q4 Q5 Q6 Q7Q8Q9
Colour - redColour - blueColour - greenShape - squareShape - circle  Children - 1 Children - 2 Children - 3 Children - 4
red blue greensquarecircle 1234

 

I have used SAS for sometime now but not for these kinds of manipulation and I would appreciate any help.Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input (Q1	Q2	Q3	Q4 	Q5	 Q6 	Q7	Q8	Q9) (:$20.);
cards;
Colour 	 . .	 	Shape 	. 	 Children	. . . 	 
red 	blue 	green	square	circle 	1	2	3	4
;

data want;
set have;
array t(*) q:;
if _n_=1 then do;
set have(firstobs=2 obs=2 rename=(q1-q9=j1-j9));
array k(*)j:;
do i=1 to dim(t);
if missing(t(I)) then t(I)=t(i-1);
end;
do i=1 to dim(t);
t(I)=catx('-',t(i),k(i));
end;
end;
drop i j:;
run;

View solution in original post

8 REPLIES 8
ballardw
Super User

@kk89 wrote:

I have the first 2 rows of data which looks as given (Q1-Q9 are the variable names)

 

Q1 Q2 Q3 Q4  Q5  Q6  Q7 Q8 Q9
Colour      Shape     Children      
red  blue  green square circle  1 2 3 4

 

I would like the rows to look as given below

 

Q1 Q2 Q3 Q4  Q5  Q6  Q7 Q8 Q9
Colour - red Colour - blue Colour - green Shape - square Shape - circle   Children - 1  Children - 2  Children - 3  Children - 4
red  blue  green square circle  1 2 3 4

 

I have used SAS for sometime now but not for these kinds of manipulation and I would appreciate any help.Thank you!


Is this only the first two lines of data of a file or does this need to be done in the middle of the data set?

And why would one value need the word colour combined with  the actual color (sorry I'm American) and others just the actual color?

 

This almost seems like you want to have a Label of 'Colour red' associated with the variable Q1 to describe the column. Is this the case?

kk89
Calcite | Level 5
This is just the first two rows of the data.

Yes, I would want a Label of 'Colour red' associated with the variable Q1 to describe the column. I'm sorry if I didn't describe this well.
Reeza
Super User
How is the file coming originally, as text? If so, can you please attach a file with a few lines that illustrate exactly how you're receiving the data.
kk89
Calcite | Level 5

The file that I received was in csv format. I have provided an example of how the data looks like in the attached file.

 

id What colours do you like?  What shapes do you like? How many children do you have?   
 red blue greensquarecircle 1234

 

 

As the csv file shows, there seems to be two headers which I would like to combine to 1 as given below. 

 

 

id What colours do you like? - redWhat colours do you like? - blueWhat colours do you like? - greenWhat shapes do you like? - squareWhat shapes do you like? - circleHow many children do you have? - 1How many children do you have? - 2How many children do you have? - 3How many children do you have? - 4

 

Please do let me know if you have any additional questions. Thanks a lot!

novinosrin
Tourmaline | Level 20
data have;
input (Q1	Q2	Q3	Q4 	Q5	 Q6 	Q7	Q8	Q9) (:$20.);
cards;
Colour 	 . .	 	Shape 	. 	 Children	. . . 	 
red 	blue 	green	square	circle 	1	2	3	4
;

data want;
set have;
array t(*) q:;
if _n_=1 then do;
set have(firstobs=2 obs=2 rename=(q1-q9=j1-j9));
array k(*)j:;
do i=1 to dim(t);
if missing(t(I)) then t(I)=t(i-1);
end;
do i=1 to dim(t);
t(I)=catx('-',t(i),k(i));
end;
end;
drop i j:;
run;
novinosrin
Tourmaline | Level 20

Hmm I missed to see your latest post and answered your original post. 

kk89
Calcite | Level 5
Thank you novinosrin. Your solution works!
novinosrin
Tourmaline | Level 20

Ok Thanks

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 2362 views
  • 0 likes
  • 4 in conversation