Reshaping Long to Wide with Multiple Variables

Reshaping Long to Wide with Multiple Variables

by Manuel Armas-Phan -
Number of replies: 2

I exported RedCAP data that was longitudinal. Each record (patient) has one or multiple rows that represent clinic visits. Each clinic visit has multiple variables associated with the clinic visit. Since each clinic visit measures the same variables, then all variables can be reshaped from long to wide form. However, when I look at help file for reshape it requires that I list all variables that will be reshaped. Is there an easier way to reshape all the variables from long to wide form? Is there a way to call for all variables in a dataset or better yet all variables after the clinic date? If I can call all or a fraction of the variables with a command, it would reduce the amount of code I would type.

In reply to Manuel Armas-Phan

Re: Reshaping Long to Wide with Multiple Variables

by Kristen -

Yes, you can definitely do this. 

the ds command lists all variables, and then you can store them in a local macro (called myvarlist or whatever you want to call it) like this: 


ds

local myvarlist `r(varlist)'

display "`myvarlist'"


Then when you reshape you can just type in myvarlist instead of individually entering each command. 



In reply to Kristen

Re: Reshaping Long to Wide with Multiple Variables

by Kristen -

Here is another example if you don't want to include all variables in your variable list: 


webuse nhanes2, clear

ds

local a = r(varlist)

local b  make sampl strata psu

local c:list  a - b

disp "`c'"