02-10-2018 04:11 AM - last edited on 02-12-2018 01:31 PM by ChrisHemedinger
So I noticed a weird behavior in SAS. By the end of intensive or long work sessions, the calculations sometimes get astray. Errors, unexpected negative signs, etc. If I restart the software, things fall into place. It doesn't sound right but it's happening, different laptops, different operating systems (different versions of Windows to be exact). Restarting the laptop doesn't add much to this observation but I restart the whole thing anyway.
Any explanation, or a setting I need to adjust?
Thanks a lot.
P.S. I'm using SAS 9.4
Edit: It doesn't happen with a specific analysis. Happened with panel analysis, and with a simple addition of two variables. Also, it happens randomly on different projects with large data.
02-10-2018 06:18 AM - edited 02-10-2018 06:20 AM
As we asked in a previous thread you created, we need to see the code and we need to see the data and an example of this "stray" calculation. Without that, it's impossible to provide any assistance.
02-10-2018 12:44 PM
Here's the latest incident
If Workers <= 25 then Size = 1;
If (Workers > 25 AND Workers <= 50) then Size = 2;
If (Workers > 50 AND Workers <= 100) then Size = 3;
If (Workers > 100 AND Workers <= 200) then Size = 4;
If (Workers > 200 AND Workers <= 300) then Size = 5;
If (Workers > 300 AND Workers <= 400) then Size = 6;
If (Workers > 400 AND Workers <= 500) then Size = 7;
If (Workers > 500) then Size = 8;
Asset = Cash + Gold;
Transfer = Total - SUM(Cash, Gold);
AssetRatio = SUM(Cash, Gold)/Total;
TransferRatio = (Total - SUM(Cash, Gold))/Total;
When the error happened, the "Transfer" randomly carried negative signs, which doesn't make sense because it should all sum to the total. I almost called off the project before I remembered the alleged glitch, restarted the software, and voila! All is good.
I'm attaching a sample of the data for your convenience.
02-10-2018 01:42 PM
What's your exact version of SAS? 9.4 TS1M3?
I had that yesterday...it confused the heck out of me. I had a query where it was working with single quotes but the exact same query wasn't working with double quotes. After a restart it worked fine.
No clue why it happens but definitely more when I'm writing/developing macros so I think it has more to do with an error or incorrect code causing an issue.
02-10-2018 03:26 PM - edited 02-10-2018 03:30 PM
I'm using SAS 9.4 TS1M4
Well, I made a habit of periodically closing the software and launching it again before testing key changes to any code. You might find this helpful.
I'm inclined to put it on RAM management issue more than a code issue. For one, the thing works fine with the same code after the restart (without RAM congestion per the Task Manager). I previously thought it's a flaw in my old CPU, but now I'm using a totally different device from a different brand, yet the problem grinned at me again!
05-16-2018 09:27 PM
>Well, I made a habit of periodically closing the software and launching it again before testing key changes to any code.
EG takes care of this for me.
10 deaths in 2 days last time I counted.
"Epitaphs" are varied too: out of memory exception, Image cannot be added to the ImageList, cannot create file that already exists, failed to rename a lockfile. So many ways to be surprised...
02-10-2018 06:14 PM
Can you provide an example of the output data with the "randomly carried negative signs"? I'm assuming here that these numbers are much below zero and are not just slightly below zero as a result of data precision issues.
02-10-2018 09:56 PM
Unfortunately, I did not save that output. It was too erroneous for my project to keep.
To your other point, Nope. They were not 'much below zero'. They were the exact correct results yet with negative signs in random order across the observations of 'Transfer' in the abovementioned code.
For example, if the correct calculation is
Then that erroneous output would have been something like:
This example is not a copy of the error, but a demonstration of how it looked with the abovementioned code and data sample. Hope it helps.
02-11-2018 01:53 AM
I'm with @Reeza that the more likely cause that you're observing such issues is some "leftover from a previous run". When using clients like SAS EG then you're constantly using the same session with whatever you've already done previously still being part of the session. So it's a good idea to close and reconnect to the server from time to time. You don't have to close the client or even restart your computer - just reconnect to the server as this will create a brand new SAS session for you.
Using the code (see below) and data you've posted I've run 10000 iterations but couldn't replicate what you describe. The result was for me always the same as one would hope for.
options nonotes nosource nomprint; libname source 'c:\temp'; %macro test(iter); %do i=1 %to &iter; data test_&i; set source.sample; if workers <= 25 then size = 1; if (workers > 25 and workers <= 50) then size = 2; if (workers > 50 and workers <= 100) then size = 3; if (workers > 100 and workers <= 200) then size = 4; if (workers > 200 and workers <= 300) then size = 5; if (workers > 300 and workers <= 400) then size = 6; if (workers > 400 and workers <= 500) then size = 7; if (workers > 500) then size = 8; asset = cash + gold; transfer = total - sum(cash, gold); assetratio = sum(cash, gold)/total; transferratio = (total - sum(cash, gold))/total; run; %if &i ne 1 %then %do; proc compare data=test_%eval(&i-1) compare=test_&i out=work.diff outnoequal noprint; run; data _null_; call symputx('nobs',nobs); stop; set work.diff nobs=nobs; run; %if &nobs>0 %then %do; data diff; ds1="test_%eval(&i-1)"; ds2="test_&i"; set diff; run; proc print data=diff; run; %let i=%eval(&iter+1); %end; %else %do; proc delete data=test_%eval(&i-1); run; %end; %end; %end; %mend; %test(10000);
02-11-2018 10:42 AM
Thanks a lot. I appreciate you all taking the time and brain cycles through this.
Probably I misread Reeza's point (sorry Reeza). Yes, 'left over' seems to be the word.
OK, I guess we all boil it down to making fresh sessions a habit.
Thank you again, everyone.
02-14-2018 03:58 AM - edited 02-15-2018 08:15 PM
On a side note in regards of the code you've posted: If you're coding a set of selections where only one of the conditions may be True then consider using the ELSE statement as then SAS will stop testing as soon as a condition becomes True.
The code for this would look like:
data test; set source.sample; if workers <= 25 then size = 1; ELSE if (workers > 25 and workers <= 50) then size = 2; ELSE if (workers > 50 and workers <= 100) then size = 3; ELSE if (workers > 100 and workers <= 200) then size = 4; ELSE if (workers > 200 and workers <= 300) then size = 5; ELSE if (workers > 300 and workers <= 400) then size = 6; ELSE if (workers > 400 and workers <= 500) then size = 7; ELSE if (workers > 500) then size = 8; asset = cash + gold; transfer = total - sum(cash, gold); assetratio = sum(cash, gold)/total; transferratio = (total - sum(cash, gold))/total; run;
Another option is to use Formats and Informats which are a very powerful alternative method to get such recoding done.
proc format; invalue workerSize low - 25 = 1 25 - 50 = 2 50 - 100 = 3 100 - 200 = 4 200 - 300 = 5 300 - 400 = 6 400 - 500 = 7 500 - high = 8 ; run; data test2; set source.sample; size=input(workers,workerSize.); asset = cash + gold; transfer = total - sum(cash, gold); assetratio = sum(cash, gold)/total; transferratio = (total - sum(cash, gold))/total; run;
02-14-2018 08:14 PM
Wow! This is a completely new one for me. I honestly cannot describe enough how I appreciate your advice; let alone that it went beyond the uneducated limitations of my initial question. Thank you. My respects!
If indeed it is a leftover, which I suspect too, I'd recommend you try to understand what is really causing it. This may "haunt" you in the future when you start writing more complex programs. Personally, I've never had the opportunity to find a bug in data step, and I wrote a lot of "fancy" stuff with it.
But if you think you really found a bug in data step, please document the way to reproduce it and get in touch with SAS Support. We'll all be thankful to you if you do so!