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

i have this data,

2001 WALKING AEROBICS TENNIS
2002 FOOTBALL BASKETBALL TENNIS
2003 AEROBICS SWIMMING CYCLING

 

and i need to convert it to like this, how?

 

Obs Id Pref Sports
1 2001 1 WALKING
2 2001 2 AEROBICS
3 2001 3 TENNIS
4 2002 1 FOOTBALL
5 2002 2 BASKETBA
6 2002 3 TENNIS
7 2003 1 AEROBICS
8 2003 2 SWIMMING
9 2003 3 CYCLING

 

is it something to do with looping, arrays or proc transpose?.

 

please show  me code. Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input year sports_list & $50.;
cards;
2001 WALKING AEROBICS TENNIS
2002 FOOTBALL BASKETBALL TENNIS
2003 AEROBICS SWIMMING CYCLING
;

data want;
 set have;
 do Pref=1 to countw(sports_list);
  sports=scan(sports_list,pref);
  output;
 end;
 keep pref sports;
run;

/*OR*/

data have;
input year (sports1-sports3) (:$30.);
cards;
2001 WALKING AEROBICS TENNIS
2002 FOOTBALL BASKETBALL TENNIS
2003 AEROBICS SWIMMING CYCLING
;

data want;
 set have;
 array t sports1-sports3;
 do Pref=1 to dim(t);
  sports=t(pref);
  output;
 end;
 keep pref sports;
run;

 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data have;
input year sports_list & $50.;
cards;
2001 WALKING AEROBICS TENNIS
2002 FOOTBALL BASKETBALL TENNIS
2003 AEROBICS SWIMMING CYCLING
;

data want;
 set have;
 do Pref=1 to countw(sports_list);
  sports=scan(sports_list,pref);
  output;
 end;
 keep pref sports;
run;

/*OR*/

data have;
input year (sports1-sports3) (:$30.);
cards;
2001 WALKING AEROBICS TENNIS
2002 FOOTBALL BASKETBALL TENNIS
2003 AEROBICS SWIMMING CYCLING
;

data want;
 set have;
 array t sports1-sports3;
 do Pref=1 to dim(t);
  sports=t(pref);
  output;
 end;
 keep pref sports;
run;

 

Raj00007
Calcite | Level 5
the solution is missing years.

can you please include years also in the code and rewrite the code again
novinosrin
Tourmaline | Level 20

My apologies. Change your keep statement to-

keep year pref sports;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 1386 views
  • 0 likes
  • 2 in conversation