Differences between revisions 2 and 3
Revision 2 as of 2022-09-25 20:15:18
Size: 257
Comment:
Revision 3 as of 2023-01-10 22:09:25
Size: 2235
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
'''`collapse`''' is ideal for aggregated statistics. The '''`collapse`''' command is ideal for aggregated statistics.
Line 23: Line 23:
----



== Reshape ==

The '''`reshape`''' command can be used to move data between wide and tall formats. It has additional, helpful features, such as a diagnostic `reshape errors` command.



=== Reshape Wide ===

To expand long data into a wide format, try:

{{{
reshape wide VARSTUB, i(KEYVAR) j(GROUPVAR)
}}}

A series of variables named like `VARSTUB*` will be created, for each group of `GROUPVAR`.

If the group indicator should be placed anywhere other than as a suffix to `VARSTUB`, use a single at siogn (`@`) to indicate the placement. For example, if the groups are `1`, `2`, and `3`, then the command `reshape wide inc@r, i(KEYVAR) j(GROUPVAR)` would create variable `inc1r`, `inc2r`, and `inc3r`.

If `GROUPVAR` is a string variable, the `string` option is mandatory.

If data has been transformed through a `reshape wide` command like above, then to restore data to the long format, try:

{{{
reshape long
}}}

The parameters of the transformation are stored and reused between calls.



=== Reshape Long ===

To contract wide data into a long format, try:

{{{
reshape long VARSTUB, i(KEYVAR) j(GROUPVAR)
}}}

...`VARSTUB` will be created from the variable list `VARSTUB*`, and `GROUPVAR` will be created to indicate the source of `VARSTUB`. If the `string` option is specified, `GROUPVAR` will be a string variable.

If the variable list does not follow the simple pattern of `VARSTUB*`, it may be possible to specify with an at sign (`@`). For example, if the target variables are `inc1r`, `inc2r`, and `inc3r` and the intended `GROUPVAR` groups are `1`, `2`, and `3`, then the command `reshape long inc@r, i(KEYVAR) j(GROUPVAR)` would correctly create the variable `incr`.

If data has been transformed through a `reshape long` command like above, then to restore data to the wide format, try:

{{{
reshape wide
}}}

The parameters of the transformation are stored and reused between calls.

Stata Aggregating Data


Contract

The collapse command is ideal for aggregated statistics.

contract VAR, freq(Count) percent(Percentage)


Collapse


Reshape

The reshape command can be used to move data between wide and tall formats. It has additional, helpful features, such as a diagnostic reshape errors command.

Reshape Wide

To expand long data into a wide format, try:

reshape wide VARSTUB, i(KEYVAR) j(GROUPVAR)

A series of variables named like VARSTUB* will be created, for each group of GROUPVAR.

If the group indicator should be placed anywhere other than as a suffix to VARSTUB, use a single at siogn (@) to indicate the placement. For example, if the groups are 1, 2, and 3, then the command reshape wide inc@r, i(KEYVAR) j(GROUPVAR) would create variable inc1r, inc2r, and inc3r.

If GROUPVAR is a string variable, the string option is mandatory.

If data has been transformed through a reshape wide command like above, then to restore data to the long format, try:

reshape long

The parameters of the transformation are stored and reused between calls.

Reshape Long

To contract wide data into a long format, try:

reshape long VARSTUB, i(KEYVAR) j(GROUPVAR)

...VARSTUB will be created from the variable list VARSTUB*, and GROUPVAR will be created to indicate the source of VARSTUB. If the string option is specified, GROUPVAR will be a string variable.

If the variable list does not follow the simple pattern of VARSTUB*, it may be possible to specify with an at sign (@). For example, if the target variables are inc1r, inc2r, and inc3r and the intended GROUPVAR groups are 1, 2, and 3, then the command reshape long inc@r, i(KEYVAR) j(GROUPVAR) would correctly create the variable incr.

If data has been transformed through a reshape long command like above, then to restore data to the wide format, try:

reshape wide

The parameters of the transformation are stored and reused between calls.


CategoryRicottone

Stata/AggregatingData (last edited 2023-06-09 16:51:50 by DominicRicottone)