BookmarkSubscribeRSS Feed
KyleS83
Calcite | Level 5
Hi, just wondering if anyone can provide some help on this SAS exercise I'm working on using Proc Transpose. Here is my code:

data test;
input sid score year;
cards;
1 89 2001
2 88 2001
1 92 2002
2 98 2003
1 99 2004
2 89 2004
;
run;

proc sort data=test;
by sid;
run;

proc transpose data=test out=test2 prefix=year;
var score;
by sid;
id year;
run;

proc print data=test2;
run;

Here is the output (looks a little weird in this thread but you get the idea):

Obs sid _NAME_ year2001 year2002 year2004 year2003

1 1 score 89 92 99 .
2 2 score 88 . 89 98


in the output, the values for variable year would become variables themselves but year2004 is before year2003. How do I put year2003 before year2004. What modifications do I need to make to my code in order to do this?

Thanks!
3 REPLIES 3
art297
Opal | Level 21
Kyle,

I'm not sure if there is any option that can do what you want, but you can always reorder the variables after having run proc transpose. For example, the following will do just that if run after the statements shown in your example:
[pre]
proc sql noprint;
select strip(name) into :vars separated by ' '
from dictionary.columns
where libname="WORK" and memname="TEST2"
order by 1
;
quit;

data want;
retain &vars.;
set test2;
run;
[/pre]
HTH,
Art
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello KyleS83,

In you case the simpliest way is to add one more datastep:
[pre]
data test2;
retain sid _NAME_ year2001-year2004;
set test2;
run;
[/pre]
Sincerely,
SPR
Ksharp
Super User
[pre]
data test;
input sid score year;
cards;
1 89 2001
2 88 2001
1 92 2002
2 98 2003
1 99 2004
2 89 2004
;
run;
proc sql ;
create table temp as
select *
from (select distinct sid from test),(select distinct year from test)
;

create table op as
select temp.*,score
from temp left join test on temp.sid = test.sid and temp.year = test.year
;
quit;
proc transpose data=op out=want prefix=year;
by sid;
id year;
var score;
run;
[/pre]

Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 662 views
  • 0 likes
  • 4 in conversation