BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
niki0209
Obsidian | Level 7

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 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

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.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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. 

--
Paige Miller
niki0209
Obsidian | Level 7

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?

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
ballardw
Super User

@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.

 

 

maguiremq
SAS Super FREQ

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.

niki0209
Obsidian | Level 7
Dude, this code is perfect. Thank you very much! (I don't actually have commas in my variable names, but I get your point ;-). )

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