Hi
I have this data:
Person | Status, january | Status, february | Status, march |
A | 2 | 3 | 2 |
B | 4 | 4 | 4 |
C | 3 | 2 | 3 |
And I need it transformed to this data:
Person | Time | Status |
A | January | 2 |
A | February | 3 |
A | March | 2 |
B | January | 4 |
B | February | 4 |
B | March | 4 |
C | January | 3 |
C | February | 2 |
C | March | 3 |
Can anyone help me do this? Thanks a lot 🙂
Some of my work has noticed increased speed from running the transposition in a data step, but I'm unsure if that always holds.
data have;
input person $1. status_january status_february status_march;
datalines;
A 2 3 2
B 4 4 4
C 3 2 3
;
run;
data want (drop = status_: i);
set have;
array _s [*] status_:;
do i = 1 to dim(_s);
status = _s[i];
time = propcase(scan(vname(_s[i]), -1, '_'));
person = person;
output;
end;
run;
Now, do you actually have commas in your variable names? I substituted with underscores to make working code.
Nonetheless, here's my output:
person status time A 2 January A 3 February A 2 March B 4 January B 4 February B 4 March C 3 January C 2 February C 3 March
PROC TRANSPOSE is generally easier to understand.
This code will perform the task for you, under an important assumption.
proc transpose data=have out=want;
by person;
run;
The assumption is that you are actually working with a valid SAS data set with valid SAS variable names; and not this apparent Excel file that you show. Valid SAS variable names cannot have commas or spaces in the variable name, so you have to fix that.
From now on, please provide data as working SAS data step code (examples and instructions) and not as Excel or Excel copy and paste into the window where you type your message.
Hi. Thanks for trying to help, but it doesn't seem to work.
(Yes, I am working with a proper SAS dataset - I just made up some variables names quickly).
When I use the code it just gives me an empty data set.
Do you know why?
@niki0209 wrote:
Hi. Thanks for trying to help, but it doesn't seem to work.
(Yes, I am working with a proper SAS dataset - I just made up some variables names quickly).
When I use the code it just gives me an empty data set.
Do you know why?
Something about your actual real SAS data set is different than the one you showed. Please provide the data as working SAS data step code, as I explained earlier. Also please show the EXACT code you used.
@niki0209 wrote:
Hi. Thanks for trying to help, but it doesn't seem to work.
(Yes, I am working with a proper SAS dataset - I just made up some variables names quickly).
When I use the code it just gives me an empty data set.
Do you know why?
We might have a chance of answering that if you include the log with the code an all of the notes from running the code. Copy the entire log text, on the forum open a text box using the </> icon that appears above the message window and paste all of the copied text.
Some of my work has noticed increased speed from running the transposition in a data step, but I'm unsure if that always holds.
data have;
input person $1. status_january status_february status_march;
datalines;
A 2 3 2
B 4 4 4
C 3 2 3
;
run;
data want (drop = status_: i);
set have;
array _s [*] status_:;
do i = 1 to dim(_s);
status = _s[i];
time = propcase(scan(vname(_s[i]), -1, '_'));
person = person;
output;
end;
run;
Now, do you actually have commas in your variable names? I substituted with underscores to make working code.
Nonetheless, here's my output:
person status time A 2 January A 3 February A 2 March B 4 January B 4 February B 4 March C 3 January C 2 February C 3 March
PROC TRANSPOSE is generally easier to understand.
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!
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.
Ready to level-up your skills? Choose your own adventure.