{"id":45,"date":"2006-09-27T19:07:23","date_gmt":"2006-09-27T23:07:23","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/sas-lag-problems\/"},"modified":"2006-09-27T19:07:23","modified_gmt":"2006-09-27T23:07:23","slug":"sas-lag-problems","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/sas-lag-problems\/","title":{"rendered":"SAS lag problems"},"content":{"rendered":"
I just found an interesting (that’s interesting in the ‘I just spent an hour debugging that<\/em>?’ sense) characteristic in SAS. If you have a variable, And here are the results:<\/p>\r\n\r\n[sas]\r\nObs id x lagid lagx duration\r\n1 a 1 . .\r\n2 a 2 a . .\r\n3 a 3 a 2 1\r\n4 b 12 a . .\r\n5 b 13 b 3 10\r\n6 b 15 b 13 2\r\n[\/sas]\r\n\r\n There are obviously some strange things going on here. I have no idea what is going on in the 2nd row of the results where Anyway, it took me two years to run into this so I guess this isn’t a common mistake but if you’re ever having trouble with a x<\/code>, and are using the
lag<\/code> of
x<\/code>, do NOT put the
lag(x)<\/code> inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to see through code than to explain:<\/p>\r\n\r\n[sas]\r\ndata test;\r\ninput id$1 x;\r\ncards;\r\n a 1\r\n a 2\r\n a 3\r\n b 12\r\n b 13\r\n b 15\r\nrun;\r\n\r\ndata test;\r\nset test;\r\n lagid=lag(id);\r\n if lagid=id then do;\r\n lagx=lag(x);\r\n duration=x-lagx;\r\n end;\r\nrun;\r\n\r\nproc print;\r\nrun;\r\n[\/sas]\r\n\r\n
lagx<\/code> is missing when it should be 1. And in the 5th row,
lagx<\/code> is the 3 from 2 rows above when it should be 12. Luckily, the solution is easy. Just execute the lag every time:<\/p>\r\n\r\n[sas]\r\ndata test;\r\nset test;\r\n lagid=lag(id);\r\n lagx=lag(x);\r\n if lagid=id then do;\r\n duration=x-lagx;\r\n end;\r\nrun;\r\n[\/sas]\r\n\r\n
lag<\/code>, check to make sure it is executed for every line of the data.<\/p>","protected":false},"excerpt":{"rendered":"I just found an interesting (that’s interesting in the ‘I just spent an hour debugging that?’ sense) characteristic in SAS. If you have a variable, x, and are using the lag of x, do NOT put the lag(x) inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to […]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12,11],"tags":[94,97,95,93,515,98,96,99,709],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/45"}],"collection":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":0,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}