SAS Programming

DATA Step, Macro, Functions and more
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

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

 

 

DrAbhijeetSafai
Lapis Lazuli | Level 10

@PaigeMiller , many thanks!

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2508 views
  • 1 like
  • 5 in conversation