Help using Base SAS procedures

'If do' question

Super Contributor
Posts: 261

'If do' question

If I want to write code that changes the value of 2 or more fields if another field has a certain value i normally write

If Field1 = 'X' then do:

  Field2 = 'Y';

  Field3 = 'Z';


Instead of using If-do is there a way you can write it like if Field1 = 'X' then Field2 = 'Y' and Field3 = 'Z'??

Super User
Super User
Posts: 9,599

Re: 'If do' question

Not that I know of.  Your if syntax above looks fine to me, and reads far better than a long string version such as you request, e.g. I can instantly see that there are two variables being manipulated and what is assigned simply by observing the indented text, it would really bug me if I started seeing code like:

if a=b then a=2 and b=5 and c=5 and d=3 and e=e and...;

Super User
Posts: 10,280

Re: 'If do' question

"and" is a logical operator and not a tool to string statements together.

If you are inclined to write unreadable spaghetti code, "if field1='X' then do;field2='Y';field3='Z';end;" in one line will do nicely.


Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation