BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
spraynardz90
Calcite | Level 5

Hi

-My main concern are the 1262 and 1272 observations in the distcd.

-First, for the same company (identified by the individual permno) I am looking to keep the observations which are one month before the 1262 or 1272. (along with keeping these observations of 1262 and 1272).

Hypothetical example: keep monthdate 201111 for permno 93417 if distcd 1262 exists in 201112 for permno 93417. (please note dates are in numeric form).

-Next, I would like to create 3 different databases....

Database 1: Contains 1262 or 1272 observations, where each firms DIVAMT is 10% or greater of the previous month's PRC.

Database 2: Contains 1262 or 1272 observations, where each firms DIVAMT is  >5% and <10% of the previous month's PRC.

Database 3: Contains all the remaining 1262 and 1272 observations.

Please note: I need the one month prior observations merely for calculation purposes. See sample data below.

DATEPERMNODISTCDDIVAMTPRCMONTHDATE
1933053110014..18193305
1933063010014..21.625193306
193307311001412320.1522.875193307
1933083110014..30.25193308
1933093010014..26.25193309
193310311001412320.1525.625193310
193310311001412720.1525.625193310
1933112910014..22.625193311
1933123010014..22.375193312
193401311001412320.1521.75193401
193401311001412720.1521.75193401
1934022810014..19.75193402
1934033110014..22.625193403
193404301001412320.1519.625193404
193404301001412720.1519.625193404
1934053110014..19.5193405
1934063010014..21.125193406
193407311001412320.1519.375193407
193407311001412720.1519.375193407
1934083110014..19193408
193409291001412320.1520.25193409
193410311001412720.1517.875193410
1934113010014..17.625193411
1934123110014..18.875193412
193501311001412320.1517.375193501
193501311001412720.1517.375193501
1935022810014..16.5193502
1935033010014..16.25193503
193504301001412320.1517.75193504
193504301001412720.1517.75193504
1935053110014..17.25193505
1935062910014..16.75193506
193507311001412320.1515.5193507
193507311001412720.1515.5193507
1935083110014..16.5193508
1935093010014..15.5193509
193510311001412320.1513.875193510
193510311001412720.1513.875193510
1935113010014..15.375193511
1935123110014..15.375193512
193601311001412320.1516.375193601
193601311001412720.1516.375193601
1936022910014..15.5193602
1936033110014..15193603
193604301001412320.1514.375193604
193604301001412720.1514.375193604
1936052910014..14.25193605
1936063010014..14.125193606
193607311001412320.1513.625193607
193607311001412720.1513.625193607
1936083110014..14.625193608
1936093010014..16.75193609
193610311001412320.1515.25193610
193610311001412720.1515.25193610
1936113010014..14.5193611
1936123110014..14.625193612
193701301001412120.1514.5193701
193701301001412720.1514.5193701
1937022710014..15193702
1937033110014..14.625193703
193704301001412120.1512.5193704
193704301001412720.1512.5193704
1937052810014..12193705
1937063010014..11.375193706
193707311001412120.15-11.6875193707
193707311001412720.15-11.6875193707
1937083110014..11.625193708
1937093010014..12.25193709
193710301001412120.1511.375193710
193710301001412720.311.375193710
1937113010014..11.625193711
1937123110014..11.5193712
193801311001412120.1512.5193801
193801311001412720.1512.5193801
1938022810014..11.75193802
1938033110014..8.75193803
193804301001412120.159.625193804
193804301001412720.159.625193804
1938053110014..9.875193805
1938063010014..10.5193806
193807301001412120.1511193807
193807301001412720.1511193807
1938083110014..10.375193808
1938093010014..9.875193809
193810311001412120.159.5193810
193810311001412720.159.5193810
1938113010014..9.875193811
193812311001412320.259.875193812
1939013110014..9.75193901
1939022810014..9.75193902
1939033110014..8.875193903
193904291001412320.258193904
1939053110014..8.5193905
193906301001412320.157.125193906
1939073110014..7.5193907
1939083110014..6.875193908
1939093010014..6.75193909
193910311001412320.156.875193910
1939113010014..6.75193911
1939123010014..6.375193912
194001311001412120.156.625194001
1940022910014..6.25194002
1940033010014..6.25194003
194004301001412120.156.125194004
1940053110014..4.625194005
1940062910014..4.5194006
194007311001412120.154.5194007
1940083110014..4.5194008
1940093010014..4.75194009
194010311001412120.154.75194010
1940113010014..5.25194011
1940123110014..4.625194012
194101311001412120.1254.625194101
1941022810014..4.5194102
1941033110014..4.375194103
194104301001412120.1254194104
1941053110014..4194105
1941063010014..4194106
194107311001412120.1254.375194107
1941083010014..4.25194108
1941093010014..3.25194109
1941103110014..2.5194110
1941112910014..2.625194111
1941123110014..1.625194112
1942013110014..2.375194201
1942022810014..2194202
1942033110014..1.75194203
198702271001612320.2271.5198702
1987033110016..72198703
1987043010016..68198704
198705291001612320.2267.5198705
1987063010016..72.5198706
1987073110016..78.5198707
198708311001612320.2279.75198708
1987093010016..74198709
1987103010016..45.5198710
198711301001612320.2349.75198711
1987123110016..53.5198712
1988012910016..56198801
198802291001612320.2358.5198802
1988033110016..63.5198803
1988042910016..70198804
198805311001612320.2371198805
1988063010016..68198806
1988072910016..67198807
198808311001612320.2366.25198808
1988093010016..67.25198809
1988103110016..79198810
198811301001612320.2573198811
198812301001612720.274198812
1989013110016..78198901
198902281001612320.378198902
1989033110016..78198903
1989042810016..74.5198904
198905311001612320.374.5198905
1989063010016..77198906
1989073110016..69198907
198908311001612320.377198908
1989092910016..78.75198909
1989103110016..76198910
198911301001612320.3-73.5198911
1989122910016..65198912
19900131100165523023199001
199002281001612320.12524199002
1990033010016..23.75199003
1990043010016..22.75199004
199005311001612320.12524.25199005
1990062910016..24.75199006
1990073110016..23199007
199008311001612320.12517.5199008
1990092810016..15.5199009
1990103110016..12199010
199011301001612320.12512199011
1990123110016..13.75199012
1991013110016..13.25199101
1927083110022..47.375192708
19270930100225533051.5192709
192709301002212320.7551.5192709
1927103110022..56192710
1927113010022..58192711
19271231100221232158.5192712
192712311002212720.2558.5192712
1928013110022..-57.1875192801
1928022910022..58192802
19280331100221232162.5192803
192803311002212720.2562.5192803
1928043010022..64192804
1928053110022..67.625192805
19280630100221232164192806
192806301002212720.2564192806
1928073110022..63192807
1928083110022..69.125192808
19280929100221232171192809
192809291002212720.2571192809
1928103110022..73.5192810
1928113010022..72.5192811
19281231100221232168192812
192812311002212720.2568192812
1929013110022..73.75192901
1929022810022..69192902
19290328100221232165192903
192903281002212720.2565192903
1929043010022..67192904
1929053110022..64.75192905
192906291002212321.192906
192906291002212720.25.192906
1929073110022..65.5192907
1929083010022..69192908
192909301002212321-64.75192909
192909301002212720.25-64.75192909
1929103110022..57192910
1929112710022..57.375192911
192912311002212321.2559192912
1930013110022..62.375193001
1930022810022..63.375193002
193003311002212321.2562.5193003
1930043010022..66.875193004
1930052910022..-65.3125193005
193006301002212321.2558.875193006
1930073110022..62.375193007
1930082910022..63.75193008
193009301002212321.2561193009
1930103110022..59.25193010
1930112910022..-58.75193011
193012311002212321.2558.5193012
1931013110022..62.75193101
1931022810022..64.375193102
193103311002212321.25-57.0625193103
1931043010022..49.75193104
1931052910022..49193105
193106301002212321.2549193106
1931073110022..49.75193107
1931083110022..48.375193108
193109301002212321.2530193109
1931103110022..34.5193110
1931113010022..31193111
193112311002212321.2523.5193112
1932013010022..-22.375193201
1932022910022..25.5193202
193203311002212320.7522.25193203
1932043010022..-18.0625193204
1932053110022..14.5193205
193206301002212320.7515193206
1932073010022..-20.6875193207
1932083110022..23.25193208
193209301002212320.75-22.625193209
1932103110022..-20193210
1932113010022...193211
193212311002212320.7522.875193212
1933013110022..21.5193301
1934073110022..52.5193407
1934083110022..-54.75193408
193409291002212321-54.25193409
1934103110022..-54.625193410
1934113010022..63.25193411
19341231100221232168193412
1935013110022..-69.6875193501
1935022810022..74.875193502
193503301002212321-67.375193503
193503301002212720.25-67.375193503
1935043010022..74193504
1935053110022..76.625193505
193506291002212321.25-82.75193506
1935073110022..93193507
1935083110022..-89.5193508
193509301002212321.2594193509
1935103110022..-91193510
1935113010022..94193511
193512311002212321.2591193512
1936013110022..-94193601
1936022910022..-111.625193602
193603311002212321.5-109.75193603
1936043010022..99193604
1936052910022..-101.75193605
193606301002212321.5-108.625193606
1936073110022..-107.6875193607
19360831100225523038.5193608
193609301002212320.5-37.25193609
1936103110022..37.75193610
193611301002212320.536193611
193611301002212720.2536193611
1936123110022..33.25193612
1937013010022..34.75193701
1937022710022..34193702
193703311002212120.531.875193703
1937043010022..29.5193704
1937052810022..-29.625193705
193706301002212120.527.75193706
1937073110022..-29.25193707
1937083110022..27193708
193709301002212120.524.25193709
1937103010022..23193710
1937113010022..18.5193711
193712311002212120.515.75193712
1938013110022..-19.5625193801
1938022810022..19.5193802
193803311002212120.515.375193803
1938043010022..-17.25193804
1938053110022..16.75193805
193806301002212120.417193806
1938073010022..18.5193807
1938083110022..-17.875193808
193809301002212120.417.25193809
1938103110022..16.875193810
1938113010022..16.25193811
193812311002212120.414.875193812
1942033110022..5.125194203
194204301002212120.255.25194204
1942052910022..5.875194205
1942063010022..-5.8125194206
1942073110022..6.375194207
1942083110022..-7.1875194208
1942093010022..7.5194209
194210311002212120.25-7.75194210
1942113010022..9.25194211
194212311002212720.25-9.1875194212
1943013010022..9.625194301
1943022710022..13194302
1943033110022..14194303
194304301002212120.513.875194304
1943052910022..14.25194305
1943063010022..14.25194306
1943073110022..13.75194307
1943083110022..-13.625194308
1943093010022..14194309
194310301002212120.5-14.25194310
1943113010022..-13.875194311
1943123110022..13.75194312
1944013110022..14.5194401
1944022910022..-14.625194402
1944033110022..16194403
194404291002212120.5-15.375194404
1944053110022..-15.75194405
1944063010022..-16.9375194406
1944073110022..16.875194407
1944083110022..-17.125194408
1944093010022..18.5194409
194410311002212120.75-16.75194410
1944113010022..-17.6875194411
1944123010022..18.5194412
1945013110022..20194501
1945022810022..-22.0625194502
1945033110022..22.5194503
194504301002212120.7524.375194504
1945053110022..-24.75194505
1945063010022..23.5194506
1945073110022..-23.375194507
1945083110022..-24.125194508
1945092910022..26194509
194510311002212120.7528.75194510
1945113010022..34194511
1945123110022..37.5194512
194601311002212320.538194601
1946022810022..37.625194602
194603301002212320.548194603
1946043010022..56.25194604
1946053110022..62.5194605
194606281002212320.559.5194606
1946073110022..56.5194607
19460830100225523019194608
194609301002212320.2516.875194609
1946103110022..15.25194610
1946113010022..15.5194611
194612311002212320.2514.75194612
194612311002212720.2514.75194612
1947013110022..14.625194701
1947022810022..14.625194702
194703311002212320.2513.875194703
1947043010022..12.875194704
1947052910022..11.875194705
194706301002212320.2511.25194706
1947073110022..12194707
1947082910022..11194708
194709301002212320.2510.375194709
1947103110022..11.25194710
1947112910022..10.125194711
1947123110022..11194712
194801311002212320.2511194801
1948022810022..10.5194802
1948033110022..10.625194803
1948043010022..10.25194804
194805281002212320.2510.625194805
1948063010022..10.375194806
1948073010022..8.75194807
194808311002212320.1258.125194808
1940083110030..-35194008
194009301003012120.4-37.125194009
1940103110030..37.5194010
1940113010030..38.75194011
194012311003012120.438194012
194012311003012720.638194012
1941013110030..35194101
1941022810030..35.125194102
194103311003012120.4-33.5194103
1941043010030..31194104
1941053110030..-35.5194105
194106301003012120.4-36.375194106
194106301003012720.1-36.375194106
1941073110030..-37.25194107
1941083010030..-35194108
194109301003012120.434194109
194109301003012720.134194109
1941103110030..32.125194110
1941112910030..30194111
194112311003012120.428.5194112
194112311003012720.428.5194112
1942013110030..31.25194201
1942022810030..-30.4375194202
194203311003012120.25-27.375194203
1942043010030..-24.6875194204
1942052910030..-24.5194205
194206301003012120.2524.75194206
194206301003012720.124.75194206
1942073110030..25.25194207
1942083110030..-25.5194208
194209301003012120.25-26.875194209
194209301003012720.1-26.875194209
1942103110030..27.5194210
1942113010030..28.5194211
194212311003012120.2527194212
194212311003012720.527194212
1943013010030..32.625194301
1943022710030..34.75194302
194303311003012120.2538194303
1943043010030..37.75194304
1943052910030..40.125194305
194306301003012120.25-39.25194306
194306301003012720.15-39.25194306
1943073110030..37.5194307
1943083110030..-36.375194308
194309301003012120.2537.25194309
194309301003012720.1537.25194309
1943103010030..-39.125194310
1943113010030..37.25194311
194312311003012120.25-39.5194312
194312311003012720.5-39.5194312
1944013110030..40194401
1944022910030..42194402
194403311003012120.2539.25194403
194403311003012720.139.25194403
1944042910030..39194404
1944053110030..40.75194405
194406301003012120.2544.5194406
194406301003012720.1544.5194406
1944073110030..43194407
1944083110030..45.25194408
194409301003012120.2542.25194409
194409301003012720.1542.25194409
1944103110030..44.25194410
1944113010030..45194411
194412301003012120.2545.75194412
194412301003012720.2545.75194412
1945013110030..44.875194501
1945022810030..46.5194502
194503311003012120.2544194503
194503311003012720.144194503
1945043010030..46194504
1945053110030..44.875194505
194506301003012120.2544.25194506
194506301003012720.1544.25194506
1945073110030..44194507
1945083110030..45194508
194509291003012120.25-51.5194509
194509291003012720.15-51.5194509
1945103110030..52.25194510
1945113010030..51.5194511
194512311003012120.2553194512
194512311003012720.2553194512
1946013110030..56194601
1946022810030..51194602
194603301003012120.2552194603
1946043010030..62194604
1946053110030..61.5194605
194606281003012120.2563194606
194606281003012720.2563194606
1946073110030..57194607
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If I understood what you mean.

data have;
infile datalines expandtabs;
input DATE : yymmdd.     PERMNO     DISTCD     DIVAMT     PRC     MONTHDATE ;
format  DATE  yymmddn8. ;
datalines;
19330531     10014     .     .     18     193305
19330630     10014     .     .     21.625     193306
19330731     10014     1232     0.15     22.875     193307
19330831     10014     .     .     30.25     193308
19330930     10014     .     .     26.25     193309
19331031     10014     1232     0.15     25.625     193310
19331031     10014     1272     0.15     25.625     193310
19331129     10014     .     .     22.625     193311
19331230     10014     .     .     22.375     193312
19340131     10014     1232     0.15     21.75     193401
19340131     10014     1272     0.15     21.75     193401
19340228     10014     .     .     19.75     193402
19340331     10014     .     .     22.625     193403
19340430     10014     1232     0.15     19.625     193404
19340430     10014     1272     0.15     19.625     193404
19340531     10014     .     .     19.5     193405
19340630     10014     .     .     21.125     193406
19340731     10014     1232     0.15     19.375     193407
19340731     10014     1272     0.15     19.375     193407
19340831     10014     .     .     19     193408
19340929     10014     1232     0.15     20.25     193409
19341031     10014     1272     0.15     17.875     193410
19341130     10014     .     .     17.625     193411
19341231     10014     .     .     18.875     193412
19350131     10014     1232     0.15     17.375     193501
19350131     10014     1272     0.15     17.375     193501
19350228     10014     .     .     16.5     193502
19350330     10014     .     .     16.25     193503
19350430     10014     1232     0.15     17.75     193504
19350430     10014     1272     0.15     17.75     193504
19350531     10014     .     .     17.25     193505
19350629     10014     .     .     16.75     193506
19350731     10014     1232     0.15     15.5     193507
19350731     10014     1272     0.15     15.5     193507
19350831     10014     .     .     16.5     193508
19350930     10014     .     .     15.5     193509
19351031     10014     1232     0.15     13.875     193510
19351031     10014     1272     0.15     13.875     193510
19351130     10014     .     .     15.375     193511
19351231     10014     .     .     15.375     193512
19360131     10014     1232     0.15     16.375     193601
19360131     10014     1272     0.15     16.375     193601
19360229     10014     .     .     15.5     193602
19360331     10014     .     .     15     193603
19360430     10014     1232     0.15     14.375     193604
19360430     10014     1272     0.15     14.375     193604
19360529     10014     .     .     14.25     193605
19360630     10014     .     .     14.125     193606
19360731     10014     1232     0.15     13.625     193607
19360731     10014     1272     0.15     13.625     193607
19360831     10014     .     .     14.625     193608
19360930     10014     .     .     16.75     193609
19361031     10014     1232     0.15     15.25     193610
19361031     10014     1272     0.15     15.25     193610
19361130     10014     .     .     14.5     193611
19361231     10014     .     .     14.625     193612
19370130     10014     1212     0.15     14.5     193701
19370130     10014     1272     0.15     14.5     193701
19370227     10014     .     .     15     193702
19370331     10014     .     .     14.625     193703
19370430     10014     1212     0.15     12.5     193704
19370430     10014     1272     0.15     12.5     193704
19370528     10014     .     .     12     193705
19370630     10014     .     .     11.375     193706
19370731     10014     1212     0.15     -11.6875     193707
19370731     10014     1272     0.15     -11.6875     193707
19370831     10014     .     .     11.625     193708
19370930     10014     .     .     12.25     193709
19371030     10014     1212     0.15     11.375     193710
19371030     10014     1272     0.3     11.375     193710
19371130     10014     .     .     11.625     193711
19371231     10014     .     .     11.5     193712
19380131     10014     1212     0.15     12.5     193801
19380131     10014     1272     0.15     12.5     193801
19380228     10014     .     .     11.75     193802
19380331     10014     .     .     8.75     193803
19380430     10014     1212     0.15     9.625     193804
19380430     10014     1272     0.15     9.625     193804
19380531     10014     .     .     9.875     193805
19380630     10014     .     .     10.5     193806
19380730     10014     1212     0.15     11     193807
19380730     10014     1272     0.15     11     193807
19380831     10014     .     .     10.375     193808
19380930     10014     .     .     9.875     193809
19381031     10014     1212     0.15     9.5     193810
19381031     10014     1272     0.15     9.5     193810
19381130     10014     .     .     9.875     193811
19381231     10014     1232     0.25     9.875     193812
19390131     10014     .     .     9.75     193901
19390228     10014     .     .     9.75     193902
19390331     10014     .     .     8.875     193903
19390429     10014     1232     0.25     8     193904
19390531     10014     .     .     8.5     193905
19390630     10014     1232     0.15     7.125     193906
19390731     10014     .     .     7.5     193907
19390831     10014     .     .     6.875     193908
19390930     10014     .     .     6.75     193909
19391031     10014     1232     0.15     6.875     193910
19391130     10014     .     .     6.75     193911
19391230     10014     .     .     6.375     193912
19400131     10014     1212     0.15     6.625     194001
19400229     10014     .     .     6.25     194002
19400330     10014     .     .     6.25     194003
19400430     10014     1212     0.15     6.125     194004
19400531     10014     .     .     4.625     194005
19400629     10014     .     .     4.5     194006
19400731     10014     1212     0.15     4.5     194007
19400831     10014     .     .     4.5     194008
19400930     10014     .     .     4.75     194009
19401031     10014     1212     0.15     4.75     194010
19401130     10014     .     .     5.25     194011
19401231     10014     .     .     4.625     194012
19410131     10014     1212     0.125     4.625     194101
19410228     10014     .     .     4.5     194102
19410331     10014     .     .     4.375     194103
19410430     10014     1212     0.125     4     194104
19410531     10014     .     .     4     194105
19410630     10014     .     .     4     194106
19410731     10014     1212     0.125     4.375     194107
19410830     10014     .     .     4.25     194108
19410930     10014     .     .     3.25     194109
19411031     10014     .     .     2.5     194110
19411129     10014     .     .     2.625     194111
19411231     10014     .     .     1.625     194112
19420131     10014     .     .     2.375     194201
19420228     10014     .     .     2     194202
19420331     10014     .     .     1.75     194203
19870227     10016     1232     0.22     71.5     198702
19870331     10016     .     .     72     198703
19870430     10016     .     .     68     198704
19870529     10016     1232     0.22     67.5     198705
19870630     10016     .     .     72.5     198706
19870731     10016     .     .     78.5     198707
19870831     10016     1232     0.22     79.75     198708
19870930     10016     .     .     74     198709
19871030     10016     .     .     45.5     198710
19871130     10016     1232     0.23     49.75     198711
19871231     10016     .     .     53.5     198712
19880129     10016     .     .     56     198801
19880229     10016     1232     0.23     58.5     198802
19880331     10016     .     .     63.5     198803
19880429     10016     .     .     70     198804
19880531     10016     1232     0.23     71     198805
19880630     10016     .     .     68     198806
19880729     10016     .     .     67     198807
19880831     10016     1232     0.23     66.25     198808
19880930     10016     .     .     67.25     198809
19881031     10016     .     .     79     198810
19881130     10016     1232     0.25     73     198811
19881230     10016     1272     0.2     74     198812
19890131     10016     .     .     78     198901
19890228     10016     1232     0.3     78     198902
19890331     10016     .     .     78     198903
19890428     10016     .     .     74.5     198904
19890531     10016     1232     0.3     74.5     198905
19890630     10016     .     .     77     198906
19890731     10016     .     .     69     198907
19890831     10016     1232     0.3     77     198908
19890929     10016     .     .     78.75     198909
19891031     10016     .     .     76     198910
19891130     10016     1232     0.3     -73.5     198911
19891229     10016     .     .     65     198912
19900131     10016     5523     0     23     199001
19900228     10016     1232     0.125     24     199002
19900330     10016     .     .     23.75     199003
19900430     10016     .     .     22.75     199004
19900531     10016     1232     0.125     24.25     199005
19900629     10016     .     .     24.75     199006
19900731     10016     .     .     23     199007
19900831     10016     1232     0.125     17.5     199008
19900928     10016     .     .     15.5     199009
19901031     10016     .     .     12     199010
19901130     10016     1232     0.125     12     199011
19901231     10016     .     .     13.75     199012
19910131     10016     .     .     13.25     199101
19270831     10022     .     .     47.375     192708
19270930     10022     5533     0     51.5     192709
19270930     10022     1232     0.75     51.5     192709
19271031     10022     .     .     56     192710
19271130     10022     .     .     58     192711
19271231     10022     1232     1     58.5     192712
19271231     10022     1272     0.25     58.5     192712
19280131     10022     .     .     -57.1875     192801
19280229     10022     .     .     58     192802
19280331     10022     1232     1     62.5     192803
19280331     10022     1272     0.25     62.5     192803
19280430     10022     .     .     64     192804
19280531     10022     .     .     67.625     192805
19280630     10022     1232     1     64     192806
19280630     10022     1272     0.25     64     192806
19280731     10022     .     .     63     192807
19280831     10022     .     .     69.125     192808
19280929     10022     1232     1     71     192809
19280929     10022     1272     0.25     71     192809
19281031     10022     .     .     73.5     192810
19281130     10022     .     .     72.5     192811
19281231     10022     1232     1     68     192812
19281231     10022     1272     0.25     68     192812
19290131     10022     .     .     73.75     192901
19290228     10022     .     .     69     192902
19290328     10022     1232     1     65     192903
19290328     10022     1272     0.25     65     192903
19290430     10022     .     .     67     192904
19290531     10022     .     .     64.75     192905
19290629     10022     1232     1     .     192906
19290629     10022     1272     0.25     .     192906
19290731     10022     .     .     65.5     192907
19290830     10022     .     .     69     192908
19290930     10022     1232     1     -64.75     192909
19290930     10022     1272     0.25     -64.75     192909
19291031     10022     .     .     57     192910
19291127     10022     .     .     57.375     192911
19291231     10022     1232     1.25     59     192912
19300131     10022     .     .     62.375     193001
19300228     10022     .     .     63.375     193002
19300331     10022     1232     1.25     62.5     193003
19300430     10022     .     .     66.875     193004
19300529     10022     .     .     -65.3125     193005
19300630     10022     1232     1.25     58.875     193006
19300731     10022     .     .     62.375     193007
19300829     10022     .     .     63.75     193008
19300930     10022     1232     1.25     61     193009
19301031     10022     .     .     59.25     193010
19301129     10022     .     .     -58.75     193011
19301231     10022     1232     1.25     58.5     193012
19310131     10022     .     .     62.75     193101
19310228     10022     .     .     64.375     193102
19310331     10022     1232     1.25     -57.0625     193103
19310430     10022     .     .     49.75     193104
19310529     10022     .     .     49     193105
19310630     10022     1232     1.25     49     193106
19310731     10022     .     .     49.75     193107
19310831     10022     .     .     48.375     193108
19310930     10022     1232     1.25     30     193109
19311031     10022     .     .     34.5     193110
19311130     10022     .     .     31     193111
19311231     10022     1232     1.25     23.5     193112
19320130     10022     .     .     -22.375     193201
19320229     10022     .     .     25.5     193202
19320331     10022     1232     0.75     22.25     193203
19320430     10022     .     .     -18.0625     193204
19320531     10022     .     .     14.5     193205
19320630     10022     1232     0.75     15     193206
19320730     10022     .     .     -20.6875     193207
19320831     10022     .     .     23.25     193208
19320930     10022     1232     0.75     -22.625     193209
19321031     10022     .     .     -20     193210
19321130     10022     .     .     .     193211
19321231     10022     1232     0.75     22.875     193212
19330131     10022     .     .     21.5     193301
19340731     10022     .     .     52.5     193407
19340831     10022     .     .     -54.75     193408
19340929     10022     1232     1     -54.25     193409
19341031     10022     .     .     -54.625     193410
19341130     10022     .     .     63.25     193411
19341231     10022     1232     1     68     193412
19350131     10022     .     .     -69.6875     193501
19350228     10022     .     .     74.875     193502
19350330     10022     1232     1     -67.375     193503
19350330     10022     1272     0.25     -67.375     193503
19350430     10022     .     .     74     193504
19350531     10022     .     .     76.625     193505
19350629     10022     1232     1.25     -82.75     193506
19350731     10022     .     .     93     193507
19350831     10022     .     .     -89.5     193508
19350930     10022     1232     1.25     94     193509
19351031     10022     .     .     -91     193510
19351130     10022     .     .     94     193511
19351231     10022     1232     1.25     91     193512
19360131     10022     .     .     -94     193601
19360229     10022     .     .     -111.625     193602
19360331     10022     1232     1.5     -109.75     193603
19360430     10022     .     .     99     193604
19360529     10022     .     .     -101.75     193605
19360630     10022     1232     1.5     -108.625     193606
19360731     10022     .     .     -107.6875     193607
19360831     10022     5523     0     38.5     193608
19360930     10022     1232     0.5     -37.25     193609
19361031     10022     .     .     37.75     193610
19361130     10022     1232     0.5     36     193611
19361130     10022     1272     0.25     36     193611
19361231     10022     .     .     33.25     193612
19370130     10022     .     .     34.75     193701
19370227     10022     .     .     34     193702
19370331     10022     1212     0.5     31.875     193703
19370430     10022     .     .     29.5     193704
19370528     10022     .     .     -29.625     193705
19370630     10022     1212     0.5     27.75     193706
19370731     10022     .     .     -29.25     193707
19370831     10022     .     .     27     193708
19370930     10022     1212     0.5     24.25     193709
19371030     10022     .     .     23     193710
19371130     10022     .     .     18.5     193711
19371231     10022     1212     0.5     15.75     193712
19380131     10022     .     .     -19.5625     193801
19380228     10022     .     .     19.5     193802
19380331     10022     1212     0.5     15.375     193803
19380430     10022     .     .     -17.25     193804
19380531     10022     .     .     16.75     193805
19380630     10022     1212     0.4     17     193806
19380730     10022     .     .     18.5     193807
19380831     10022     .     .     -17.875     193808
19380930     10022     1212     0.4     17.25     193809
19381031     10022     .     .     16.875     193810
19381130     10022     .     .     16.25     193811
19381231     10022     1212     0.4     14.875     193812
19420331     10022     .     .     5.125     194203
19420430     10022     1212     0.25     5.25     194204
19420529     10022     .     .     5.875     194205
19420630     10022     .     .     -5.8125     194206
19420731     10022     .     .     6.375     194207
19420831     10022     .     .     -7.1875     194208
19420930     10022     .     .     7.5     194209
19421031     10022     1212     0.25     -7.75     194210
19421130     10022     .     .     9.25     194211
19421231     10022     1272     0.25     -9.1875     194212
19430130     10022     .     .     9.625     194301
19430227     10022     .     .     13     194302
19430331     10022     .     .     14     194303
19430430     10022     1212     0.5     13.875     194304
19430529     10022     .     .     14.25     194305
19430630     10022     .     .     14.25     194306
19430731     10022     .     .     13.75     194307
19430831     10022     .     .     -13.625     194308
19430930     10022     .     .     14     194309
19431030     10022     1212     0.5     -14.25     194310
19431130     10022     .     .     -13.875     194311
19431231     10022     .     .     13.75     194312
19440131     10022     .     .     14.5     194401
19440229     10022     .     .     -14.625     194402
19440331     10022     .     .     16     194403
19440429     10022     1212     0.5     -15.375     194404
19440531     10022     .     .     -15.75     194405
19440630     10022     .     .     -16.9375     194406
19440731     10022     .     .     16.875     194407
19440831     10022     .     .     -17.125     194408
19440930     10022     .     .     18.5     194409
19441031     10022     1212     0.75     -16.75     194410
19441130     10022     .     .     -17.6875     194411
19441230     10022     .     .     18.5     194412
19450131     10022     .     .     20     194501
19450228     10022     .     .     -22.0625     194502
19450331     10022     .     .     22.5     194503
19450430     10022     1212     0.75     24.375     194504
19450531     10022     .     .     -24.75     194505
19450630     10022     .     .     23.5     194506
19450731     10022     .     .     -23.375     194507
19450831     10022     .     .     -24.125     194508
19450929     10022     .     .     26     194509
19451031     10022     1212     0.75     28.75     194510
19451130     10022     .     .     34     194511
19451231     10022     .     .     37.5     194512
19460131     10022     1232     0.5     38     194601
19460228     10022     .     .     37.625     194602
19460330     10022     1232     0.5     48     194603
19460430     10022     .     .     56.25     194604
19460531     10022     .     .     62.5     194605
19460628     10022     1232     0.5     59.5     194606
19460731     10022     .     .     56.5     194607
19460830     10022     5523     0     19     194608
19460930     10022     1232     0.25     16.875     194609
19461031     10022     .     .     15.25     194610
19461130     10022     .     .     15.5     194611
19461231     10022     1232     0.25     14.75     194612
19461231     10022     1272     0.25     14.75     194612
19470131     10022     .     .     14.625     194701
19470228     10022     .     .     14.625     194702
19470331     10022     1232     0.25     13.875     194703
19470430     10022     .     .     12.875     194704
19470529     10022     .     .     11.875     194705
19470630     10022     1232     0.25     11.25     194706
19470731     10022     .     .     12     194707
19470829     10022     .     .     11     194708
19470930     10022     1232     0.25     10.375     194709
19471031     10022     .     .     11.25     194710
19471129     10022     .     .     10.125     194711
19471231     10022     .     .     11     194712
19480131     10022     1232     0.25     11     194801
19480228     10022     .     .     10.5     194802
19480331     10022     .     .     10.625     194803
19480430     10022     .     .     10.25     194804
19480528     10022     1232     0.25     10.625     194805
19480630     10022     .     .     10.375     194806
19480730     10022     .     .     8.75     194807
19480831     10022     1232     0.125     8.125     194808
19400831     10030     .     .     -35     194008
19400930     10030     1212     0.4     -37.125     194009
19401031     10030     .     .     37.5     194010
19401130     10030     .     .     38.75     194011
19401231     10030     1212     0.4     38     194012
19401231     10030     1272     0.6     38     194012
19410131     10030     .     .     35     194101
19410228     10030     .     .     35.125     194102
19410331     10030     1212     0.4     -33.5     194103
19410430     10030     .     .     31     194104
19410531     10030     .     .     -35.5     194105
19410630     10030     1212     0.4     -36.375     194106
19410630     10030     1272     0.1     -36.375     194106
19410731     10030     .     .     -37.25     194107
19410830     10030     .     .     -35     194108
19410930     10030     1212     0.4     34     194109
19410930     10030     1272     0.1     34     194109
19411031     10030     .     .     32.125     194110
19411129     10030     .     .     30     194111
19411231     10030     1212     0.4     28.5     194112
19411231     10030     1272     0.4     28.5     194112
19420131     10030     .     .     31.25     194201
19420228     10030     .     .     -30.4375     194202
19420331     10030     1212     0.25     -27.375     194203
19420430     10030     .     .     -24.6875     194204
19420529     10030     .     .     -24.5     194205
19420630     10030     1212     0.25     24.75     194206
19420630     10030     1272     0.1     24.75     194206
19420731     10030     .     .     25.25     194207
19420831     10030     .     .     -25.5     194208
19420930     10030     1212     0.25     -26.875     194209
19420930     10030     1272     0.1     -26.875     194209
19421031     10030     .     .     27.5     194210
19421130     10030     .     .     28.5     194211
19421231     10030     1212     0.25     27     194212
19421231     10030     1272     0.5     27     194212
19430130     10030     .     .     32.625     194301
19430227     10030     .     .     34.75     194302
19430331     10030     1212     0.25     38     194303
19430430     10030     .     .     37.75     194304
19430529     10030     .     .     40.125     194305
19430630     10030     1212     0.25     -39.25     194306
19430630     10030     1272     0.15     -39.25     194306
19430731     10030     .     .     37.5     194307
19430831     10030     .     .     -36.375     194308
19430930     10030     1212     0.25     37.25     194309
19430930     10030     1272     0.15     37.25     194309
19431030     10030     .     .     -39.125     194310
19431130     10030     .     .     37.25     194311
19431231     10030     1212     0.25     -39.5     194312
19431231     10030     1272     0.5     -39.5     194312
19440131     10030     .     .     40     194401
19440229     10030     .     .     42     194402
19440331     10030     1212     0.25     39.25     194403
19440331     10030     1272     0.1     39.25     194403
19440429     10030     .     .     39     194404
19440531     10030     .     .     40.75     194405
19440630     10030     1212     0.25     44.5     194406
19440630     10030     1272     0.15     44.5     194406
19440731     10030     .     .     43     194407
19440831     10030     .     .     45.25     194408
19440930     10030     1212     0.25     42.25     194409
19440930     10030     1272     0.15     42.25     194409
19441031     10030     .     .     44.25     194410
19441130     10030     .     .     45     194411
19441230     10030     1212     0.25     45.75     194412
19441230     10030     1272     0.25     45.75     194412
19450131     10030     .     .     44.875     194501
19450228     10030     .     .     46.5     194502
19450331     10030     1212     0.25     44     194503
19450331     10030     1272     0.1     44     194503
19450430     10030     .     .     46     194504
19450531     10030     .     .     44.875     194505
19450630     10030     1212     0.25     44.25     194506
19450630     10030     1272     0.15     44.25     194506
19450731     10030     .     .     44     194507
19450831     10030     .     .     45     194508
19450929     10030     1212     0.25     -51.5     194509
19450929     10030     1272     0.15     -51.5     194509
19451031     10030     .     .     52.25     194510
19451130     10030     .     .     51.5     194511
19451231     10030     1212     0.25     53     194512
19451231     10030     1272     0.25     53     194512
19460131     10030     .     .     56     194601
19460228     10030     .     .     51     194602
19460330     10030     1212     0.25     52     194603
19460430     10030     .     .     62     194604
19460531     10030     .     .     61.5     194605
19460628     10030     1212     0.25     63     194606
19460628     10030     1272     0.25     63     194606
19460731     10030     .     .     57     194607
;
run;

%let list= 1262 1272  ;
proc sql;
create table temp as
 select permno,date,DISTCD,intnx('year',date,-2,'s') as prev_two_year format=yymmddn8.,intnx('year',date,2,'s') as next_two_year format=yymmddn8.
  from have 
   where DISTCD in ( &list )
    order by permno,date;
quit;
data group;
 set temp;
 if permno ne lag(permno) or (permno eq lag(permno) and prev_two_year gt lag(next_two_year)) then group+1;
run;
data date_range;
 set group;
 by group;
 retain start;
 if first.group then start=prev_two_year;
 if last.group then do;end=next_two_year; output;end;
 keep permno start end;
 format  start end yymmddn8.;
run;
proc sql;
create table want as
select * from have
except
 select a.*
  from have as a,date_range as b
   where  a.permno=b.permno and a.date between b.start and b.end ;
quit;


Ksharp

View solution in original post

16 REPLIES 16
Patrick
Opal | Level 21

The data you've posted don't match your narrative. There is neither a permno= 93417 nor a  distcd= 1262.

Suggest you align data and text (eg. are in the aligne data also all these missings?) and also best post your data as csv attachement.

The selection your after should be doable with a single SQL statement and then a data step to create the 3 output datasets you're after.

I don't understand why you need this month variable. You might want read a bit about SAS dates, formats and informats and the intnx() calendar function.

spraynardz90
Calcite | Level 5

The narrative was  hypothetical. If you would like a real example it is much the same.....

For example:keep monthdate 193309 for permno 10014 (one month prior), as distcd 1272 exists in 193310 for permno 10014.

Yes I have dabbled with SAS dates but I much prefer to keep my dates as integers as the numbers are convenient to add, subtract, multiply, divide as I see fit.

The SQL sounds promising, as requested a sample of my data is attached in CSV format

art297
Opal | Level 21

Thus the earliest records for each id go into the third dataset?  What do you want to do if data for a previous month are missing?  What do you want to do if a previous month's PRC is a negative value?  Also, how do you want to define previous month?  e.g., previous calendar month?

spraynardz90
Calcite | Level 5

Sorry this is an error with the data source from CRSP. A firms stock price cannot be negative. Disregard or delete any negative PRC.

Those 1262 and 1272 distributions with no 1 month prior PRC (missing or negative) delete, they cant be allocated into any dataset.

Previous month is previous calender month, the column monthdate provides the source to refer back to.

The third dataset should just contain the 1262 and 1272 observations, which:

1) Has a previous calender month PRC to compare the current DIVAMT to

2) The DIVAMT is less than 5% of previous months PRC

art297
Opal | Level 21

Not sure if I correctly understand what you are trying to do but, if not, I think you should be able to modify the following code to accomplish what you want:

data want1 want2 want3;

  set sample;

  by permno monthdate;

  retain last_date last_prc;

  if prc gt 0 then do;

    if first.monthdate then do;

      last_date=lag(date);

      last_prc=lag(prc);

    end;

    if distcd in (1262,1272) then do;

      if intck('month',input(put(last_date,8.),yymmdd8.),input(put(date,8.),yymmdd8.)) eq 1

       then do;

        if divamt/last_prc ge 0.1 then output want1;

        else if divamt/last_prc ge 0.05 then output want2;

        else output want3;

      end;

      else output want3;

    end;

  end;

run;

Patrick
Opal | Level 21

Looking at your data I understand the key is {permno,date}. Is this correct?

If so then the first thing to do is cleaning up the data as there would be duplicates.

proc sql;

  create table DupKeys as

  select *, count(*) as dups

  from sample

  group by permno,date

  having count(*)>1

  ;

quit;

If I understood the key right then once your data is cleaned up below SQL creates a table with all rows having a non missing distcd in the current month and a missing distcd in the previous month (the lag1_<variable name> come from the previous month).

I believe once you've got this result table it's fairly simple to split it up in the way you want to as you can do all selections on row level in a data step.

I've added variable MonthEndDate which always contains the date of the last day of the month (your date variable seems to be a bit inconsistent in this regard).

proc sql;

  create table want as

  select

      intnx('month',input(put(l.date,8.),yymmdd8.),0,'e') as MonthEndDate format=yymmdd10.,

      l.*,

      r.date as lag1_date,

      r.prc as lag1_prc

  from sample l,

    (select * from sample where distcd is null) r

  where l.permno=r.permno

    and intck('month',input(put(r.date,8.),yymmdd8.),input(put(l.date,8.),yymmdd8.))=1

    and l.distcd not is null

  order by l.permno, l.date

    ;

quit;

And to insist a bit more of how to deal with dates:

All people with SAS experience here in this communities use SAS dates and not date strings (whether they are now stored in a numeric or a character variable) when it comes to any calculations with dates. There must be a reason for this....

spraynardz90
Calcite | Level 5

Thanks everyone.

In a new separate step, I am looking to perform the following:

For all those permnos which were linked to a 1262 or 1272.... I would like to delete the observations two years before and two years after the 1262 or 1272.

EG: Permno 10022 in 19421231 had a 1272. I would like to delete any observation of 10022 from 19401231 to 19441231.

Ksharp
Super User

If I understood what you mean.

data have;
infile datalines expandtabs;
input DATE : yymmdd.     PERMNO     DISTCD     DIVAMT     PRC     MONTHDATE ;
format  DATE  yymmddn8. ;
datalines;
19330531     10014     .     .     18     193305
19330630     10014     .     .     21.625     193306
19330731     10014     1232     0.15     22.875     193307
19330831     10014     .     .     30.25     193308
19330930     10014     .     .     26.25     193309
19331031     10014     1232     0.15     25.625     193310
19331031     10014     1272     0.15     25.625     193310
19331129     10014     .     .     22.625     193311
19331230     10014     .     .     22.375     193312
19340131     10014     1232     0.15     21.75     193401
19340131     10014     1272     0.15     21.75     193401
19340228     10014     .     .     19.75     193402
19340331     10014     .     .     22.625     193403
19340430     10014     1232     0.15     19.625     193404
19340430     10014     1272     0.15     19.625     193404
19340531     10014     .     .     19.5     193405
19340630     10014     .     .     21.125     193406
19340731     10014     1232     0.15     19.375     193407
19340731     10014     1272     0.15     19.375     193407
19340831     10014     .     .     19     193408
19340929     10014     1232     0.15     20.25     193409
19341031     10014     1272     0.15     17.875     193410
19341130     10014     .     .     17.625     193411
19341231     10014     .     .     18.875     193412
19350131     10014     1232     0.15     17.375     193501
19350131     10014     1272     0.15     17.375     193501
19350228     10014     .     .     16.5     193502
19350330     10014     .     .     16.25     193503
19350430     10014     1232     0.15     17.75     193504
19350430     10014     1272     0.15     17.75     193504
19350531     10014     .     .     17.25     193505
19350629     10014     .     .     16.75     193506
19350731     10014     1232     0.15     15.5     193507
19350731     10014     1272     0.15     15.5     193507
19350831     10014     .     .     16.5     193508
19350930     10014     .     .     15.5     193509
19351031     10014     1232     0.15     13.875     193510
19351031     10014     1272     0.15     13.875     193510
19351130     10014     .     .     15.375     193511
19351231     10014     .     .     15.375     193512
19360131     10014     1232     0.15     16.375     193601
19360131     10014     1272     0.15     16.375     193601
19360229     10014     .     .     15.5     193602
19360331     10014     .     .     15     193603
19360430     10014     1232     0.15     14.375     193604
19360430     10014     1272     0.15     14.375     193604
19360529     10014     .     .     14.25     193605
19360630     10014     .     .     14.125     193606
19360731     10014     1232     0.15     13.625     193607
19360731     10014     1272     0.15     13.625     193607
19360831     10014     .     .     14.625     193608
19360930     10014     .     .     16.75     193609
19361031     10014     1232     0.15     15.25     193610
19361031     10014     1272     0.15     15.25     193610
19361130     10014     .     .     14.5     193611
19361231     10014     .     .     14.625     193612
19370130     10014     1212     0.15     14.5     193701
19370130     10014     1272     0.15     14.5     193701
19370227     10014     .     .     15     193702
19370331     10014     .     .     14.625     193703
19370430     10014     1212     0.15     12.5     193704
19370430     10014     1272     0.15     12.5     193704
19370528     10014     .     .     12     193705
19370630     10014     .     .     11.375     193706
19370731     10014     1212     0.15     -11.6875     193707
19370731     10014     1272     0.15     -11.6875     193707
19370831     10014     .     .     11.625     193708
19370930     10014     .     .     12.25     193709
19371030     10014     1212     0.15     11.375     193710
19371030     10014     1272     0.3     11.375     193710
19371130     10014     .     .     11.625     193711
19371231     10014     .     .     11.5     193712
19380131     10014     1212     0.15     12.5     193801
19380131     10014     1272     0.15     12.5     193801
19380228     10014     .     .     11.75     193802
19380331     10014     .     .     8.75     193803
19380430     10014     1212     0.15     9.625     193804
19380430     10014     1272     0.15     9.625     193804
19380531     10014     .     .     9.875     193805
19380630     10014     .     .     10.5     193806
19380730     10014     1212     0.15     11     193807
19380730     10014     1272     0.15     11     193807
19380831     10014     .     .     10.375     193808
19380930     10014     .     .     9.875     193809
19381031     10014     1212     0.15     9.5     193810
19381031     10014     1272     0.15     9.5     193810
19381130     10014     .     .     9.875     193811
19381231     10014     1232     0.25     9.875     193812
19390131     10014     .     .     9.75     193901
19390228     10014     .     .     9.75     193902
19390331     10014     .     .     8.875     193903
19390429     10014     1232     0.25     8     193904
19390531     10014     .     .     8.5     193905
19390630     10014     1232     0.15     7.125     193906
19390731     10014     .     .     7.5     193907
19390831     10014     .     .     6.875     193908
19390930     10014     .     .     6.75     193909
19391031     10014     1232     0.15     6.875     193910
19391130     10014     .     .     6.75     193911
19391230     10014     .     .     6.375     193912
19400131     10014     1212     0.15     6.625     194001
19400229     10014     .     .     6.25     194002
19400330     10014     .     .     6.25     194003
19400430     10014     1212     0.15     6.125     194004
19400531     10014     .     .     4.625     194005
19400629     10014     .     .     4.5     194006
19400731     10014     1212     0.15     4.5     194007
19400831     10014     .     .     4.5     194008
19400930     10014     .     .     4.75     194009
19401031     10014     1212     0.15     4.75     194010
19401130     10014     .     .     5.25     194011
19401231     10014     .     .     4.625     194012
19410131     10014     1212     0.125     4.625     194101
19410228     10014     .     .     4.5     194102
19410331     10014     .     .     4.375     194103
19410430     10014     1212     0.125     4     194104
19410531     10014     .     .     4     194105
19410630     10014     .     .     4     194106
19410731     10014     1212     0.125     4.375     194107
19410830     10014     .     .     4.25     194108
19410930     10014     .     .     3.25     194109
19411031     10014     .     .     2.5     194110
19411129     10014     .     .     2.625     194111
19411231     10014     .     .     1.625     194112
19420131     10014     .     .     2.375     194201
19420228     10014     .     .     2     194202
19420331     10014     .     .     1.75     194203
19870227     10016     1232     0.22     71.5     198702
19870331     10016     .     .     72     198703
19870430     10016     .     .     68     198704
19870529     10016     1232     0.22     67.5     198705
19870630     10016     .     .     72.5     198706
19870731     10016     .     .     78.5     198707
19870831     10016     1232     0.22     79.75     198708
19870930     10016     .     .     74     198709
19871030     10016     .     .     45.5     198710
19871130     10016     1232     0.23     49.75     198711
19871231     10016     .     .     53.5     198712
19880129     10016     .     .     56     198801
19880229     10016     1232     0.23     58.5     198802
19880331     10016     .     .     63.5     198803
19880429     10016     .     .     70     198804
19880531     10016     1232     0.23     71     198805
19880630     10016     .     .     68     198806
19880729     10016     .     .     67     198807
19880831     10016     1232     0.23     66.25     198808
19880930     10016     .     .     67.25     198809
19881031     10016     .     .     79     198810
19881130     10016     1232     0.25     73     198811
19881230     10016     1272     0.2     74     198812
19890131     10016     .     .     78     198901
19890228     10016     1232     0.3     78     198902
19890331     10016     .     .     78     198903
19890428     10016     .     .     74.5     198904
19890531     10016     1232     0.3     74.5     198905
19890630     10016     .     .     77     198906
19890731     10016     .     .     69     198907
19890831     10016     1232     0.3     77     198908
19890929     10016     .     .     78.75     198909
19891031     10016     .     .     76     198910
19891130     10016     1232     0.3     -73.5     198911
19891229     10016     .     .     65     198912
19900131     10016     5523     0     23     199001
19900228     10016     1232     0.125     24     199002
19900330     10016     .     .     23.75     199003
19900430     10016     .     .     22.75     199004
19900531     10016     1232     0.125     24.25     199005
19900629     10016     .     .     24.75     199006
19900731     10016     .     .     23     199007
19900831     10016     1232     0.125     17.5     199008
19900928     10016     .     .     15.5     199009
19901031     10016     .     .     12     199010
19901130     10016     1232     0.125     12     199011
19901231     10016     .     .     13.75     199012
19910131     10016     .     .     13.25     199101
19270831     10022     .     .     47.375     192708
19270930     10022     5533     0     51.5     192709
19270930     10022     1232     0.75     51.5     192709
19271031     10022     .     .     56     192710
19271130     10022     .     .     58     192711
19271231     10022     1232     1     58.5     192712
19271231     10022     1272     0.25     58.5     192712
19280131     10022     .     .     -57.1875     192801
19280229     10022     .     .     58     192802
19280331     10022     1232     1     62.5     192803
19280331     10022     1272     0.25     62.5     192803
19280430     10022     .     .     64     192804
19280531     10022     .     .     67.625     192805
19280630     10022     1232     1     64     192806
19280630     10022     1272     0.25     64     192806
19280731     10022     .     .     63     192807
19280831     10022     .     .     69.125     192808
19280929     10022     1232     1     71     192809
19280929     10022     1272     0.25     71     192809
19281031     10022     .     .     73.5     192810
19281130     10022     .     .     72.5     192811
19281231     10022     1232     1     68     192812
19281231     10022     1272     0.25     68     192812
19290131     10022     .     .     73.75     192901
19290228     10022     .     .     69     192902
19290328     10022     1232     1     65     192903
19290328     10022     1272     0.25     65     192903
19290430     10022     .     .     67     192904
19290531     10022     .     .     64.75     192905
19290629     10022     1232     1     .     192906
19290629     10022     1272     0.25     .     192906
19290731     10022     .     .     65.5     192907
19290830     10022     .     .     69     192908
19290930     10022     1232     1     -64.75     192909
19290930     10022     1272     0.25     -64.75     192909
19291031     10022     .     .     57     192910
19291127     10022     .     .     57.375     192911
19291231     10022     1232     1.25     59     192912
19300131     10022     .     .     62.375     193001
19300228     10022     .     .     63.375     193002
19300331     10022     1232     1.25     62.5     193003
19300430     10022     .     .     66.875     193004
19300529     10022     .     .     -65.3125     193005
19300630     10022     1232     1.25     58.875     193006
19300731     10022     .     .     62.375     193007
19300829     10022     .     .     63.75     193008
19300930     10022     1232     1.25     61     193009
19301031     10022     .     .     59.25     193010
19301129     10022     .     .     -58.75     193011
19301231     10022     1232     1.25     58.5     193012
19310131     10022     .     .     62.75     193101
19310228     10022     .     .     64.375     193102
19310331     10022     1232     1.25     -57.0625     193103
19310430     10022     .     .     49.75     193104
19310529     10022     .     .     49     193105
19310630     10022     1232     1.25     49     193106
19310731     10022     .     .     49.75     193107
19310831     10022     .     .     48.375     193108
19310930     10022     1232     1.25     30     193109
19311031     10022     .     .     34.5     193110
19311130     10022     .     .     31     193111
19311231     10022     1232     1.25     23.5     193112
19320130     10022     .     .     -22.375     193201
19320229     10022     .     .     25.5     193202
19320331     10022     1232     0.75     22.25     193203
19320430     10022     .     .     -18.0625     193204
19320531     10022     .     .     14.5     193205
19320630     10022     1232     0.75     15     193206
19320730     10022     .     .     -20.6875     193207
19320831     10022     .     .     23.25     193208
19320930     10022     1232     0.75     -22.625     193209
19321031     10022     .     .     -20     193210
19321130     10022     .     .     .     193211
19321231     10022     1232     0.75     22.875     193212
19330131     10022     .     .     21.5     193301
19340731     10022     .     .     52.5     193407
19340831     10022     .     .     -54.75     193408
19340929     10022     1232     1     -54.25     193409
19341031     10022     .     .     -54.625     193410
19341130     10022     .     .     63.25     193411
19341231     10022     1232     1     68     193412
19350131     10022     .     .     -69.6875     193501
19350228     10022     .     .     74.875     193502
19350330     10022     1232     1     -67.375     193503
19350330     10022     1272     0.25     -67.375     193503
19350430     10022     .     .     74     193504
19350531     10022     .     .     76.625     193505
19350629     10022     1232     1.25     -82.75     193506
19350731     10022     .     .     93     193507
19350831     10022     .     .     -89.5     193508
19350930     10022     1232     1.25     94     193509
19351031     10022     .     .     -91     193510
19351130     10022     .     .     94     193511
19351231     10022     1232     1.25     91     193512
19360131     10022     .     .     -94     193601
19360229     10022     .     .     -111.625     193602
19360331     10022     1232     1.5     -109.75     193603
19360430     10022     .     .     99     193604
19360529     10022     .     .     -101.75     193605
19360630     10022     1232     1.5     -108.625     193606
19360731     10022     .     .     -107.6875     193607
19360831     10022     5523     0     38.5     193608
19360930     10022     1232     0.5     -37.25     193609
19361031     10022     .     .     37.75     193610
19361130     10022     1232     0.5     36     193611
19361130     10022     1272     0.25     36     193611
19361231     10022     .     .     33.25     193612
19370130     10022     .     .     34.75     193701
19370227     10022     .     .     34     193702
19370331     10022     1212     0.5     31.875     193703
19370430     10022     .     .     29.5     193704
19370528     10022     .     .     -29.625     193705
19370630     10022     1212     0.5     27.75     193706
19370731     10022     .     .     -29.25     193707
19370831     10022     .     .     27     193708
19370930     10022     1212     0.5     24.25     193709
19371030     10022     .     .     23     193710
19371130     10022     .     .     18.5     193711
19371231     10022     1212     0.5     15.75     193712
19380131     10022     .     .     -19.5625     193801
19380228     10022     .     .     19.5     193802
19380331     10022     1212     0.5     15.375     193803
19380430     10022     .     .     -17.25     193804
19380531     10022     .     .     16.75     193805
19380630     10022     1212     0.4     17     193806
19380730     10022     .     .     18.5     193807
19380831     10022     .     .     -17.875     193808
19380930     10022     1212     0.4     17.25     193809
19381031     10022     .     .     16.875     193810
19381130     10022     .     .     16.25     193811
19381231     10022     1212     0.4     14.875     193812
19420331     10022     .     .     5.125     194203
19420430     10022     1212     0.25     5.25     194204
19420529     10022     .     .     5.875     194205
19420630     10022     .     .     -5.8125     194206
19420731     10022     .     .     6.375     194207
19420831     10022     .     .     -7.1875     194208
19420930     10022     .     .     7.5     194209
19421031     10022     1212     0.25     -7.75     194210
19421130     10022     .     .     9.25     194211
19421231     10022     1272     0.25     -9.1875     194212
19430130     10022     .     .     9.625     194301
19430227     10022     .     .     13     194302
19430331     10022     .     .     14     194303
19430430     10022     1212     0.5     13.875     194304
19430529     10022     .     .     14.25     194305
19430630     10022     .     .     14.25     194306
19430731     10022     .     .     13.75     194307
19430831     10022     .     .     -13.625     194308
19430930     10022     .     .     14     194309
19431030     10022     1212     0.5     -14.25     194310
19431130     10022     .     .     -13.875     194311
19431231     10022     .     .     13.75     194312
19440131     10022     .     .     14.5     194401
19440229     10022     .     .     -14.625     194402
19440331     10022     .     .     16     194403
19440429     10022     1212     0.5     -15.375     194404
19440531     10022     .     .     -15.75     194405
19440630     10022     .     .     -16.9375     194406
19440731     10022     .     .     16.875     194407
19440831     10022     .     .     -17.125     194408
19440930     10022     .     .     18.5     194409
19441031     10022     1212     0.75     -16.75     194410
19441130     10022     .     .     -17.6875     194411
19441230     10022     .     .     18.5     194412
19450131     10022     .     .     20     194501
19450228     10022     .     .     -22.0625     194502
19450331     10022     .     .     22.5     194503
19450430     10022     1212     0.75     24.375     194504
19450531     10022     .     .     -24.75     194505
19450630     10022     .     .     23.5     194506
19450731     10022     .     .     -23.375     194507
19450831     10022     .     .     -24.125     194508
19450929     10022     .     .     26     194509
19451031     10022     1212     0.75     28.75     194510
19451130     10022     .     .     34     194511
19451231     10022     .     .     37.5     194512
19460131     10022     1232     0.5     38     194601
19460228     10022     .     .     37.625     194602
19460330     10022     1232     0.5     48     194603
19460430     10022     .     .     56.25     194604
19460531     10022     .     .     62.5     194605
19460628     10022     1232     0.5     59.5     194606
19460731     10022     .     .     56.5     194607
19460830     10022     5523     0     19     194608
19460930     10022     1232     0.25     16.875     194609
19461031     10022     .     .     15.25     194610
19461130     10022     .     .     15.5     194611
19461231     10022     1232     0.25     14.75     194612
19461231     10022     1272     0.25     14.75     194612
19470131     10022     .     .     14.625     194701
19470228     10022     .     .     14.625     194702
19470331     10022     1232     0.25     13.875     194703
19470430     10022     .     .     12.875     194704
19470529     10022     .     .     11.875     194705
19470630     10022     1232     0.25     11.25     194706
19470731     10022     .     .     12     194707
19470829     10022     .     .     11     194708
19470930     10022     1232     0.25     10.375     194709
19471031     10022     .     .     11.25     194710
19471129     10022     .     .     10.125     194711
19471231     10022     .     .     11     194712
19480131     10022     1232     0.25     11     194801
19480228     10022     .     .     10.5     194802
19480331     10022     .     .     10.625     194803
19480430     10022     .     .     10.25     194804
19480528     10022     1232     0.25     10.625     194805
19480630     10022     .     .     10.375     194806
19480730     10022     .     .     8.75     194807
19480831     10022     1232     0.125     8.125     194808
19400831     10030     .     .     -35     194008
19400930     10030     1212     0.4     -37.125     194009
19401031     10030     .     .     37.5     194010
19401130     10030     .     .     38.75     194011
19401231     10030     1212     0.4     38     194012
19401231     10030     1272     0.6     38     194012
19410131     10030     .     .     35     194101
19410228     10030     .     .     35.125     194102
19410331     10030     1212     0.4     -33.5     194103
19410430     10030     .     .     31     194104
19410531     10030     .     .     -35.5     194105
19410630     10030     1212     0.4     -36.375     194106
19410630     10030     1272     0.1     -36.375     194106
19410731     10030     .     .     -37.25     194107
19410830     10030     .     .     -35     194108
19410930     10030     1212     0.4     34     194109
19410930     10030     1272     0.1     34     194109
19411031     10030     .     .     32.125     194110
19411129     10030     .     .     30     194111
19411231     10030     1212     0.4     28.5     194112
19411231     10030     1272     0.4     28.5     194112
19420131     10030     .     .     31.25     194201
19420228     10030     .     .     -30.4375     194202
19420331     10030     1212     0.25     -27.375     194203
19420430     10030     .     .     -24.6875     194204
19420529     10030     .     .     -24.5     194205
19420630     10030     1212     0.25     24.75     194206
19420630     10030     1272     0.1     24.75     194206
19420731     10030     .     .     25.25     194207
19420831     10030     .     .     -25.5     194208
19420930     10030     1212     0.25     -26.875     194209
19420930     10030     1272     0.1     -26.875     194209
19421031     10030     .     .     27.5     194210
19421130     10030     .     .     28.5     194211
19421231     10030     1212     0.25     27     194212
19421231     10030     1272     0.5     27     194212
19430130     10030     .     .     32.625     194301
19430227     10030     .     .     34.75     194302
19430331     10030     1212     0.25     38     194303
19430430     10030     .     .     37.75     194304
19430529     10030     .     .     40.125     194305
19430630     10030     1212     0.25     -39.25     194306
19430630     10030     1272     0.15     -39.25     194306
19430731     10030     .     .     37.5     194307
19430831     10030     .     .     -36.375     194308
19430930     10030     1212     0.25     37.25     194309
19430930     10030     1272     0.15     37.25     194309
19431030     10030     .     .     -39.125     194310
19431130     10030     .     .     37.25     194311
19431231     10030     1212     0.25     -39.5     194312
19431231     10030     1272     0.5     -39.5     194312
19440131     10030     .     .     40     194401
19440229     10030     .     .     42     194402
19440331     10030     1212     0.25     39.25     194403
19440331     10030     1272     0.1     39.25     194403
19440429     10030     .     .     39     194404
19440531     10030     .     .     40.75     194405
19440630     10030     1212     0.25     44.5     194406
19440630     10030     1272     0.15     44.5     194406
19440731     10030     .     .     43     194407
19440831     10030     .     .     45.25     194408
19440930     10030     1212     0.25     42.25     194409
19440930     10030     1272     0.15     42.25     194409
19441031     10030     .     .     44.25     194410
19441130     10030     .     .     45     194411
19441230     10030     1212     0.25     45.75     194412
19441230     10030     1272     0.25     45.75     194412
19450131     10030     .     .     44.875     194501
19450228     10030     .     .     46.5     194502
19450331     10030     1212     0.25     44     194503
19450331     10030     1272     0.1     44     194503
19450430     10030     .     .     46     194504
19450531     10030     .     .     44.875     194505
19450630     10030     1212     0.25     44.25     194506
19450630     10030     1272     0.15     44.25     194506
19450731     10030     .     .     44     194507
19450831     10030     .     .     45     194508
19450929     10030     1212     0.25     -51.5     194509
19450929     10030     1272     0.15     -51.5     194509
19451031     10030     .     .     52.25     194510
19451130     10030     .     .     51.5     194511
19451231     10030     1212     0.25     53     194512
19451231     10030     1272     0.25     53     194512
19460131     10030     .     .     56     194601
19460228     10030     .     .     51     194602
19460330     10030     1212     0.25     52     194603
19460430     10030     .     .     62     194604
19460531     10030     .     .     61.5     194605
19460628     10030     1212     0.25     63     194606
19460628     10030     1272     0.25     63     194606
19460731     10030     .     .     57     194607
;
run;

%let list= 1262 1272  ;
proc sql;
create table temp as
 select permno,date,DISTCD,intnx('year',date,-2,'s') as prev_two_year format=yymmddn8.,intnx('year',date,2,'s') as next_two_year format=yymmddn8.
  from have 
   where DISTCD in ( &list )
    order by permno,date;
quit;
data group;
 set temp;
 if permno ne lag(permno) or (permno eq lag(permno) and prev_two_year gt lag(next_two_year)) then group+1;
run;
data date_range;
 set group;
 by group;
 retain start;
 if first.group then start=prev_two_year;
 if last.group then do;end=next_two_year; output;end;
 keep permno start end;
 format  start end yymmddn8.;
run;
proc sql;
create table want as
select * from have
except
 select a.*
  from have as a,date_range as b
   where  a.permno=b.permno and a.date between b.start and b.end ;
quit;


Ksharp

spraynardz90
Calcite | Level 5

Thanks Ksharp I can understand the process and what you are doing is correct. However with this code, my want database produces the same number of observations as my have database. Surely the number would decrease by a few thousands at least?

Ksharp
Super User

On behalf of Theory ,It should be smaller table.

Not seeing your real data and Log , It is hard to suggest you something.

Ksharp

spraynardz90
Calcite | Level 5

Hi I have attached my sample data (i am sorry this is not the whole database, too big to upload). With the log there seems to be an error with the intnx....

You will notice I have a few more variables and year is referred to as fyear.


errorlog.JPG
Ksharp
Super User

I can't believe your INTNX can't work. Is your DATE variable SAS DATE type ? I highly  skeptic it is CHARACTER.

Here is the LOG I runned the data you posted at the first . I have to leave now.

485  %let list= 1262 1272  ;

486  proc sql;

487  create table temp as

488   select permno,date,DISTCD,intnx('year',date,-2,'s') as prev_two_year

488! format=yymmddn8.,intnx('year',date,2,'s') as next_two_year format=yymmddn8.

489    from have

490     where DISTCD in ( &list )

491      order by permno,date;

NOTE: Table WORK.TEMP created, with 53 rows and 5 columns.

492  quit;

NOTE: PROCEDURE SQL used (Total process time):

      real time           1.35 seconds

      cpu time            0.04 seconds

493  data group;

494   set temp;

495   if permno ne lag(permno) or (permno eq lag(permno) and prev_two_year gt lag(next_two_year)) then

495!  group+1;

496  run;

NOTE: There were 53 observations read from the data set WORK.TEMP.

NOTE: The data set WORK.GROUP has 53 observations and 6 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.02 seconds

497  data date_range;

498   set group;

499   by group;

500   retain start;

501   if first.group then start=prev_two_year;

502   if last.group then do;end=next_two_year; output;end;

503   keep permno start end;

504   format  start end yymmddn8.;

505  run;

NOTE: There were 53 observations read from the data set WORK.GROUP.

NOTE: The data set WORK.DATE_RANGE has 6 observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.02 seconds

      cpu time            0.01 seconds

506  proc sql;

507  create table want as

508  select * from have

509  except

510   select a.*

511    from have as a,date_range as b

512     where  a.permno=b.permno and a.date between b.start and b.end ;

NOTE: Table WORK.WANT created, with 36 rows and 6 columns.

513  quit;

NOTE: PROCEDURE SQL used (Total process time):

      real time           0.06 seconds

      cpu time            0.01 seconds

Ksharp

spraynardz90
Calcite | Level 5

no my dates are not in sas format they are numeric

spraynardz90
Calcite | Level 5

can we tweak the code to account for this?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 16 replies
  • 1524 views
  • 6 likes
  • 5 in conversation