DATA Step, Macro, Functions and more

Converting from wide to long and creating new variable

Reply
Contributor
Posts: 20

Converting from wide to long and creating new variable

Hi, I reshaped my dataset from long to wide and now need to change it back to long. The code I used to convert from long to wide is:

 

proc sort data=medicaid_new;
	by ParticipantID;
run;

proc transpose data=medicaid_new out=medicaid_wide1 prefix=HEDIS;
	by ParticipantID;
	id Time;
	var HEDIS;
run;

The time variable has values ranging from -15 to 15. Is it possible to reshape it from wide to long and to create a new "Time" variable with its original values? In wide format, the HEDIS variables are called: HEDIS_15 for Time = -15, HEDIS_14 for Time = -14, ... HEDIS15 for Time = 15. Any help would be greatly appreciated.

Super User
Posts: 19,878

Re: Converting from wide to long and creating new variable

No, SAS variables need to start with a letter or _. 

 

You can assign the label though, using the IDLABEL statement.

Contributor
Posts: 20

Re: Converting from wide to long and creating new variable

Hi Reeza, maybe my post isn't clear. Essentially, I want the dataset to look like this:
ID HEDIS Time
1 40 -15
1 35 -14
1 35 -13
...
1 36 14
1 37 15

Where the Time variable is based on the HEDIS_15 and etc. variables in the wide dataset (HEDIS_15 is Time -15 and HEDIS15 is Time 15).
Super User
Posts: 19,878

Re: Converting from wide to long and creating new variable

Yeah, sorry, didn't catch that.

 

Use COMPRESS() within a second data step to remove the letters then and keep the numbers and - symbol.

 

 

Respected Advisor
Posts: 4,936

Re: Converting from wide to long and creating new variable

Simpler to use a data step in ths case:

 

data medicaid_long;
set medicaid_wide;
array h hedis_15 -- hedis15;
do time = -15 to 15;
	HEDIS = h{time};
	output;
	end;
keep participantID time HEDIS;
run;
PG
Ask a Question
Discussion stats
  • 4 replies
  • 65 views
  • 2 likes
  • 3 in conversation