Write and run SAS programs in your web browser

IF-THEN/DELETE on SAS university edition not working?

Reply
Occasional Contributor syk
Occasional Contributor
Posts: 19

IF-THEN/DELETE on SAS university edition not working?

What am I doing wrong? The statement is exactly the same format from the documentations I've read and the DELETE statement should be blue but it's greyed out.

 

Capture.GIF

 

Super User
Super User
Posts: 6,718

Re: IF-THEN/DELETE on SAS university edition not working?

What is the question? Are you really talking about what COLOR the text is in the program editor?

If you want to see if the code works, run it.

Occasional Contributor syk
Occasional Contributor
Posts: 19

Re: IF-THEN/DELETE on SAS university edition not working?

I've ran the code the delete statement is definitely not processing through, and as you can see from the captured image the delete statement is not even recognized as a sas statement.

Regular Contributor
Posts: 226

Re: IF-THEN/DELETE on SAS university edition not working?

By the way, your sum statement should be 

Total=sum(qtr1,qtr2,qtr3,qtr4);
Super User
Super User
Posts: 6,718

Re: IF-THEN/DELETE on SAS university edition not working?

[ Edited ]
syk wrote:

I've ran the code the delete statement is definitely not processing through, and as you can see from the captured image the delete statement is not even recognized as a sas statement.


You posted a photograph of an editor screen. The color coding that an editor does is just the editor's attempt to make your code look pretty. The editor does not actually contain the SAS parser/compiler or the SAS macro processor.

 

If you want to see whether the SAS code is correct you need to actually run the code.  

 

If you want to tell whether the DELETE statement ran we would need to see the LOG to see how many observations the data step read in and how many it wrote. We would also need to see the data since it could very well be that no obervations meet the IF condition so that the DELETE statement will never run.

 

If you had really typed something other than delete (perhaps de1ete) then you would have seen an error message in the SAS log that the statement was invalid.

Occasional Contributor syk
Occasional Contributor
Posts: 19

Re: IF-THEN/DELETE on SAS university edition not working?

[ Edited ]

I've tried both conditions for the IF statement to produce 50 observations (the question wanted 50) but when I ran it again with the counter-logic it produced 88 observations (initial number of observations) so the delete statement did not work. And no I did not spell delete incorrectly. And as you can see for some reason when the delete statement is within the IF statement it's not picked up by the compiler, but by itself it is..

Capture.GIF

Super User
Super User
Posts: 6,718

Re: IF-THEN/DELETE on SAS university edition not working?


syk wrote:

I've tried both conditions for the IF statement to produce 50 observations (the question wanted 50) but when I ran it again with the counter-logic it produced 88 observations (initial number of observations) so the delete statement did not work. And no I did not spell delete incorrectly.


That is a totally different question.

Why would you expect that running that data step would produce 50 observations?

There is nothing in the code that would do that. It might do that if there are exactly 50 observations that do NOT get deleted by that IF statement. But we would need to see the data.

 

Super User
Super User
Posts: 6,718

Re: IF-THEN/DELETE on SAS university edition not working?

Ignore the colors. Look at the log. SAS will put an erorr message in the log is you ask it to run invalid code. Until you actually submit the code to run the editor is just an editor. It doesn't really know whether you are typing SAS code or your shopping list.

Occasional Contributor syk
Occasional Contributor
Posts: 19

Re: IF-THEN/DELETE on SAS university edition not working?

You're right the statement is really meaningless I was just simply showing the compiler is not picking up the delete statement, and the log had no errors. 

 

if Total>=50 and NumQtrs=4; <--- this statement produced 50 observations like the questioni wanted.

so shouldn't 

 

if Total<50 and NumQtrs^=4 then delete; 

 

produce 50 observations as well?

Super User
Super User
Posts: 6,718

Re: IF-THEN/DELETE on SAS university edition not working?


syk wrote:

You're right the statement is really meaningless I was just simply showing the compiler is not picking up the delete statement, and the log had no errors. 

 

if Total>=50 and NumQtrs=4; <--- this statement produced 50 observations like the questioni wanted.

so shouldn't 

 

if Total<50 and NumQtrs^=4 then delete; 

 

produce 50 observations as well?


No. Apply DeMorgan's law.  https://en.wikipedia.org/wiki/De_Morgan%27s_laws

NOT (A and B) is the same as (NOT A) or (NOT B).

Super User
Posts: 7,124

Re: IF-THEN/DELETE on SAS university edition not working?

DeMorgan is one of the foundations of programming. As soon as one touches the first composite condition, one does not only have to know DeMorgan's laws, one needs to internalize them to the point where the hands automatically type the correct conversion.

 

@syk: the coloring in the SAS enhanced editor is not complete; some keywords are not caught correctly. Follow my Maxims 4 and 2.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 18,603

Re: IF-THEN/DELETE on SAS university edition not working?


 

if Total>=50 and NumQtrs=4; <--- this statement produced 50 observations like the questioni wanted.

so shouldn't 

 

if Total<50 and NumQtrs^=4 then delete; 

 

produce 50 observations as well?


No, you have two conditions joined by an AND so you theoretically have 4 different possibilities (2X2)

 

Total >= 50 and NumQtrs=4

Total < 50 and NumQtrs=4

Total >= 50 and NumQtrs^=4

Total < 50 and NumQtrs^=4

Super User
Posts: 10,889

Re: IF-THEN/DELETE on SAS university edition not working?

If Total is missing then Total<50 is True.

If NumQtrs is missing then NumQtrs ^= 4 is True.

 

So if you have missing values for the selection variables you may have more or fewer results than you think you should.

Ask a Question
Discussion stats
  • 12 replies
  • 191 views
  • 5 likes
  • 6 in conversation