Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: help on creating a new variable


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: st: help on creating a new variable
Date   Thu, 19 Jan 2006 22:55:35 +0000

If I understand this correctly, it is one
step away from the problem discussed at length in

http://www.stata.com/support/faqs/data/missing.html

You go

gen NewPkgNo = PkgNo if PieceCode == "NON"
replace NewPkgNo = NewPkgNo[_n-1]
	if missing(NewPkgNo) & PieceCode == "MPS"

Nick
[email protected]

>>> Pinaki Mitra

I have a dataset like the following (Original Data) and need to create
an additional variable (New Data). The data set is by pkgNo and
customer. Some of the pkgs. are in sequence which indicate they are from
the same source and I need to flag them by the first pkgNo. For example,
the first 4 obs. are from one source which have sequence no 1 to 4 and
ALWAYS identified by NON for the first pkgNo and MPS for the subsequent
ones. I need to create the flag "NewPkgNo" which will contain the value
of the first sequence for the entire source ("1ZA20F980340070869" when
Sequence == 1 & PieceCode[1]== "NON" & PieceCode[2] to PieceCode[4] ==
"MPS"). Obs. 5 and 6 are from the different sources since their sequence
numbers are 1 and PieceCode == "NON".

Original Data
-------------
Customer PkgNo             Sequence   PieceCode    Rate
A20F98   1ZA20F980340070869   1        NON          2.8
A20F98   1ZA20F980341667473   2        MPS          3.8
A20F98   1ZA20F980340721281   3        MPS          4.0
A20F98   1ZA20F980340376299   4        MPS          5.2
A20F98   1ZA20F980340721282   1        NON          3.0
A20F98   1ZA20F980340376200   1        NON          6.8
XX1122   1ZXX11220340721282   1        NON          3.0
XX1122   1ZXX11220340376200   1        NON          6.8
XX1133   1ZXX11330340721284   1        NON          3.0
XX1133   1ZXX11330340376255   2        MPS          6.8

New Data
--------
Customer NewPkgNo           PkgNo               Sequence   PieceCode
Rate
A20F98   1ZA20F980340070869 1ZA20F980340070869   1          NON
2.8
A20F98   1ZA20F980340070869 1ZA20F980341667473   2          MPS
3.8        A20F98   1ZA20F980340070869 1ZA20F980340721281   3
MPS         4.0        A20F98   1ZA20F980340070869 1ZA20F980340376299
4          MPS         5.2
A20F98   1ZA20F980340721282 1ZA20F980340721282   1          NON
3.0        A20F98   1ZA20F980340376200 1ZA20F980340376200   1
NON         6.8
XX1122   1ZXX11220340721282 1ZXX11220340721282   1          NON
3.0
XX1122   1ZXX11220340376200 1ZXX11220340376200   1          NON
6.8
XX1133   1ZXX11330340721284 1ZXX11330340721284   1          NON
3.0
XX1133   1ZXX11330340721284 1ZXX11330340376255   2          MPS
6.8

I can't figure out a way to tackle this problem. Can someone please
provide me any help?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index