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

Here is what my data looks like currently,

IDtimepointageeducationsurvey1survey2survey3
1126 345
12 BS456
12  567
2125 345
22 BA456
23  567

 

I want the data to by wide format by ID.

IDageeducationsurvey1_1survey1_2survey1_3survey2_1survey2_2survey2_3survey3_1survey3_2survey3_3
126BS345456567
225BA345456567
            

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Please post data step to display your data .

 

data have;
infile cards expandtabs truncover;
input ID	timepoint	age	education $	survey1	survey2	survey3;
cards;
1	1	26	 .	3	4	5
1	2	 .	BS	4	5	6
1	3	 .	 .	5	6	7
2	1	25	 .	3	4	5
2	2	 .	BA	4	5	6
2	3	 .	 .	5	6	7
;

proc sql;
create table temp as
select id,timepoint,max(age) as age,max(education) as education,survey1,survey2,survey3
 from have 
  group by id
   order by 1,2,3,4;

select max(_n) into : n
 from (select id,count(*) as _n from have group by id);
quit;

proc summary data=temp;
by id age education;
output out=want idgroup(out[&n] (survey1	survey2	survey3)=);
run;

View solution in original post

5 REPLIES 5
Ksharp
Super User

Please post data step to display your data .

 

data have;
infile cards expandtabs truncover;
input ID	timepoint	age	education $	survey1	survey2	survey3;
cards;
1	1	26	 .	3	4	5
1	2	 .	BS	4	5	6
1	3	 .	 .	5	6	7
2	1	25	 .	3	4	5
2	2	 .	BA	4	5	6
2	3	 .	 .	5	6	7
;

proc sql;
create table temp as
select id,timepoint,max(age) as age,max(education) as education,survey1,survey2,survey3
 from have 
  group by id
   order by 1,2,3,4;

select max(_n) into : n
 from (select id,count(*) as _n from have group by id);
quit;

proc summary data=temp;
by id age education;
output out=want idgroup(out[&n] (survey1	survey2	survey3)=);
run;
Nrjn7
Fluorite | Level 6

@Ksharp  thank you for your help.  it worked.

 

Quick question: If the table had up to survey10, how would the code change? 

What does the order by 1,2,3,4 statement does in this code?

 

Thank you so much!!

PaigeMiller
Diamond | Level 26

@Nrjn7 wrote:

 

Quick question: If the table had up to survey10, how would the code change? 

What does the order by 1,2,3,4 statement does in this code?


A good reason not to convert a long data set to wide. SAS coding is much easier when you keep long data sets long instead of turning them into wide. Using a wide format data set, if your number of variables changes, then your code has to change. With a long data set, this is not true, SAS coding doesn't care if you have variables up to survey3 or survey10.

 

Just because you can convert it to long doesn't mean you should. See Maxim 19 "Long beats Wide".

--
Paige Miller
Ksharp
Super User
"Quick question: If the table had up to survey10, how would the code change? "
select id,timepoint,max(age) as age,max(education) as education,survey1,survey2,survey3
-->
select id,timepoint,max(age) as age,max(education) as education,survey1,survey2,survey3,survey4,survey5,survey6,........,survey10


output out=want idgroup(out[&n] (survey1 survey2 survey3)=);
--->
output out=want idgroup(out[&n] (survey1 - survey10)=);
Ksharp
Super User
"What does the order by 1,2,3,4 statement does in this code?"

it stands for order by id, timepoint, age, education

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
  • 5 replies
  • 737 views
  • 1 like
  • 3 in conversation