Desktop productivity for business analysts and programmers

Possible to write IF/THEN/DO on one line?

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Possible to write IF/THEN/DO on one line?

From my searches seems the answer is NO but wanted to confirm...can I write the following on one line?

DATA ...

IF field1='testing' THEN

DO;

     field2='testing';

     field3='testing';

END;


Accepted Solutions
Solution
‎09-18-2013 05:38 PM
Respected Advisor
Posts: 4,606

Re: Possible to write IF/THEN/DO on one line?

You can write a whole SAS program on one line, as long as the STATEMENTS are ended by semicolons. I don't know if EG imposes a stricter format. - PG

PG

View solution in original post


All Replies
Solution
‎09-18-2013 05:38 PM
Respected Advisor
Posts: 4,606

Re: Possible to write IF/THEN/DO on one line?

You can write a whole SAS program on one line, as long as the STATEMENTS are ended by semicolons. I don't know if EG imposes a stricter format. - PG

PG
Trusted Advisor
Posts: 1,050

Re: Possible to write IF/THEN/DO on one line?

EG actually submits the code to SAS, so any rules that apply to SAS also apply to EG.

However, EG does have a nice code formatting utility...I find it great when I've hacked out some code, and can pretty it up before posting (I don't know if Display Manager has the same; I'm far from expert with it.)

Therefore, no problem submitting anything as one line...just don't make me maintain it!!

Tom

Community Manager
Posts: 2,691

Re: Possible to write IF/THEN/DO on one line?

More about the EG code formatter:

Hope for ugly programs - The SAS Dummy

Chris

Super Contributor
Posts: 418

Re: Possible to write IF/THEN/DO on one line?

It is important to point out that Macros actually have a distinct line length that cannot be passed.

For example, if you wrapped your code into a macro and then put it all on one line (and it was a long code) it would actually fail because sas wouldstop compiling it after the Xth position (It depends on versions I believe).

So yeah, don't put your entire code on one line if you are wrapping it in a macro.

Source of Knowledge: I made this mistake once before.

Grand Advisor
Posts: 10,196

Re: Possible to write IF/THEN/DO on one line?

Since SAS uses the semicolon as a statment ender it doesn't really care too much.An entire program could be on one line with only the white space between variables and functions to allow proper parsing by the complier.

However since people will be involved with maintaing the code it is a good idea to adopt some set of practices that use readibility as a criterion.

For example compare quick understanding of the code

if i = var1 then do; var1=3; m= catx(' ', var2,var3, v10); end; else do; var1=var3; end;

with

if i = var1 then

     do;

          var1=3;

          m= catx(' ', var2,var3, v10);

     end;

else

     do;

     var1=var3;

end;

And the complier wouldn't care if you had:

if i =

     var1

               then do; var1=3; m=

catx(' ', var2,var3, v10); end;

else

               do;

     var1=

                         var3;

          end;

Regular Contributor
Posts: 220

Re: Possible to write IF/THEN/DO on one line?

@80,

i think your code will do nothing except creating 3 variable field1, field2 and field3...if you simply want to create 3 field with same value,

data want;

field1='string';

field2='string';

field3='string';

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 530 views
  • 0 likes
  • 7 in conversation