BookmarkSubscribeRSS Feed
eer_seer
Obsidian | Level 7

I feel like the solution to this problem is very simple, yet, I can't quite figure out how to accomplish this simply either through PROC SQL or PROC TRANSPOSE.  I've searched the forums, but I can't seem to find a simple solution.  Currently, the data is arranged where each row is a unique observation tied to a unique id with each test measurement as a column.  What I need is to have each measurement (column with test_score1, test_score2, test_score3, etc.)

collapsed down onto the unique id as a row... so multiple test observations are showing as rows instead of as columns.  

 

Data is currently arranged like Table A:

 

Table A:

IDtest_score1test_score2test_score3test_score4
0000130.0030.0031.0027.00
0000229.0030.0030.0027.00
0000325.0033.0020.0025.00
0000426.0035.0036.0019.00
0000520.0018.0020.0021.00
0000621.0018.0022.0026.00

 

I want to convert the table to look like Table B:

 

Table B:

IDmeasurescore
00001test_score130.00
00001test_score230.00
00001test_score331.00
00001test_score427.00
00002test_score129.00
00002test_score230.00
00002test_score330.00
00002test_score427.00
00002test_score125.00
00003test_score233.00
00003test_score320.00
00003test_score425.00
00004test_score126.00
00004test_score235.00
00004test_score336.00
00004test_score419.00
00005test_score120.00
00006test_score218.00
00006test_score320.00
00006test_score421.00
00007test_score121.00
00007test_score218.00
00007test_score322.00
00007test_score426.00

Thanks!

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc transpose data=a out=b;
by id;
var test_score:;
run;
--
Paige Miller
eer_seer
Obsidian | Level 7

Thank you.

 

One question:  I see the ":" after the test_score in the "VAR" statement and I assume that acts as a function to call up all of the vars that start with "test_score".  I named each test score simply with a number, but, if I need to transpose a differently named test for each ID, do I simply call each one out, like so?:

 

proc transpose data=scores
out=test;
by id;
var
ACT_COMP_SCORE
ACT_READ_SCORE
ACT_ENG_SCORE
ACT_MATH_SCORE
SATI_COMP_SCORE
SATI_MATH_SCORE
SATI_VERB_SCORE
SATR_MATH_SCORE
SATR_VERB_SCORE
SCA;
run;

PaigeMiller
Diamond | Level 26

Yes

--
Paige Miller
ballardw
Super User

@eer_seer wrote:

Thank you.

 

One question:  I see the ":" after the test_score in the "VAR" statement and I assume that acts as a function to call up all of the vars that start with "test_score".  I named each test score simply with a number, but, if I need to transpose a differently named test for each ID, do I simply call each one out, like so?:

 

proc transpose data=scores
out=test;
by id;
var
ACT_COMP_SCORE
ACT_READ_SCORE
ACT_ENG_SCORE
ACT_MATH_SCORE
SATI_COMP_SCORE
SATI_MATH_SCORE
SATI_VERB_SCORE
SATR_MATH_SCORE
SATR_VERB_SCORE
SCA;
run;


The list might also be made as

Var Act: SAT: SCA ;

 

but the order would tend to be alphabetic by variable name and use all variables whose names start with the specific characters. So if you had other variables like ACTBB they would be included. But you could use ACT_: which would skip any variables that didn't have the _.

 

Or if the variables are contiguous, adjacent column order numbers, you could use:

VAR ACT_COMP_SCORE -- SCA;

Note that is 2 dash characters.

Plus you can mix the lists with both the : for some variables and a -- for others.  Plus if you have variable names that have a common name with a numerical suffix you can use  Var1 - Var10 as a list which would exclude Var11, Var12, Var13 (if such variables exist).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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