BookmarkSubscribeRSS Feed
joceychin
Calcite | Level 5

Hi

I was given some codes with highly nested if-then statements from an uncommon program. My purpose is to understand and document the logics behind the codes.

I am trying to match IF-THEN-ELSE but i cannot use NOTE++ to indent it properly as this program is not supported in NOte++. A typical example is :

=============================
IF MM5_1 = 1 THEN
weightfill := 'CM'
ELSEIF MM5_1 = 2 THEN
weightfill := 'KM'
ENDIF

IF MM6 <> 1 THEN
Print 1
IF MMa <> 1 THEN
Print 2
ENDIF
ENDIF

=====================

The only way i can try to understand the logics is to
tag a code say

===================
001: IF MM5_1 = 1 THEN
weightfill := 'CM'
001: ELSEIF MM5_1 = 2 THEN
weightfill := 'KM'
001: ENDIF

002: IF MM6 <> 1 THEN
Print 1
003: IF MMa <> 1 THEN
Print 2
003: ENDIF
002: ENDIF

===========================

So that i can match the IF - THEN - ELSe.

I can create each line as a record in a dataset.
Can someone give me some idea how i can tag the IF THEN ELSE properly as another field in the SAS dataset ?

Thank you
Best Regards
Jocey

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First off, what software are you using, as this doesn't look like base SAS (i.e. there are missing semicolons, := I don't recognise).

 

secondly, I see this as one example of where bad programming practices are holding you back.  Code formatting, and good programming practices have been around for a long time now.  Gone are the dark old days of all code on one line with no space, and try to obfuscate the code, now we are in an age where code readability is more important than what it does.  So if I take you code, and apply some simple rules to it - i.e. consitent casing (SAS tend to use lower case, SQL upper case), indent the code for blocking (and note I use Base SAS syntax here), also if your using multiple if statements then there are better constructs, one being select() in base SAS:

 

select(mm5_1);
  when (1) weightfill="CM";
  when (2) weightfill="KM";
  otherwise weightfill="";
end;

 

 

As you will see from the above, I can easily see that all the indented code is part of the select block.  The select() construct reduces the amount of code as it is specifically designed as a construct to make conditional processing.

 

As for further help, not knowing what software that code comes from doesn't help.

Astounding
PROC Star

Clearly, this isn't a SAS program.  However, you can make up your own rules on spacing even without an indent key.  For example:


=============================
IF     MM5_1 = 1 THEN weightfill := 'CM'
ELSEIF MM5_1 = 2 THEN weightfill := 'KM'
ENDIF

 

 

Use whatever is visually appealing and clear to you.

 

Good luck.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

With regards to this:

Use whatever is visually appealing and clear to you.

 

I would suggest first checking is there is an established guide already.  For instance if I was using C, I would check Google for:

c good programming practices

Which returns at result 1 a comprehensive document detailing common procatices.  Personally I would follow the general standards for the language you use, it makes moving between companies easier, and programmers picking up your code will be far happier.

Astounding
PROC Star

I guess I'm biased, coming from a SAS background where there are many sets of acceptable standards.

 

When I pick up the work of another programmer, I'm less concerned with the style that was used.  I do appreciate any style that I find easy to read.  But I am much more concerned with consistency.  Pick a style and stick to it, and your programs will be much easier for me to work with down the road.

 

FWIW.

ballardw
Super User

If you have access to Enterprise Guide this blog might point you to some help to make the programs look a little nicer:

 

http://blogs.sas.com/content/sasdummy/2010/07/14/hope-for-ugly-programs/

 

 

joceychin
Calcite | Level 5

Hi

 

Thanks for all the replies. It is from an uncommon program and i am trying to extract the logics out.

I know the codes are hard to read, thats why i have trouble reading it. I am trying to tag a number to each IF-THEN-ELSE-ENDIF pair. This is just a simple case. There are a few "big blocks" and i kind of lost tracing some of the IF-ENDIF pairs. 

The codes are indented in it's native viewer but the viewer cannot be edited. It allows cut and paste but all lines are left justified in an editor

 

As such, I want to find a way to decifer the logic blocks of these codes. I cannot help much dictating the style but i can only try my best to make do with that is available.

 

So, i hope to find a way to tag a number to each IF-THEN-ELSE-ENDIF pair by importing the codes into a data set and tag a number to the pair of if-then-else to make tracking of the blocks easier.

 

Best Regards

Jocey

Astounding
PROC Star

OK, your explanation makes a big difference.  Here's an approach you can take.

 

data _null_;

infile 'current_program.txt';

file 'revised_program.txt' noprint;

input @;

if _infile_ =: 'IF' then do;

   n + 1;

   put n z3. ': ' @;

end;

else if _infile_ =: 'ELSEIF' then put n z3. ': ' @;

put _infile_;

run;

 

The nested ENDIF labels don't quite match up this way, but it may be close enough for readability (or it may give you enough of an idea about how to improve the logic).

 

Good luck.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1386 views
  • 1 like
  • 4 in conversation