DATA Step, Macro, Functions and more

if-else

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

if-else

The following SAS program is submitted:


data work.new;
length word $7;
amount = 4;
it amount = 4 then word = ‘FOUR’;
else if amount = 7
then word = ‘SEVEN’;
else word = ‘NONE!!!’;
amount = 7;
run;
What are the values of the AMOUNT and WORD variables in SAS dataset work.new?

 

A. amount word
4 FOUR


B. amount word
4 NONE!!!


C. amount word
7 FOUR


D. amount word
7 SEVEN

 

The answer is C, I do not quite understand why.


Accepted Solutions
Solution
‎09-27-2017 05:49 AM
Super Contributor
Posts: 354

Re: if-else

Hello,

 

amount is first set to 4 with the instruction

 

amount=4;

 

Then  word is set to 'FOUR' with the instruction

 

it amount = 4 then word = ‘FOUR’;

 

lines begining by else can be ignored because the first if condition was met.

 

The last instruction before run sets amount to 7.

 

So in the end amount=7 and word='FOUR' (answer C)

View solution in original post


All Replies
Super User
Posts: 10,530

Re: if-else

The if condition is evaluated before amount is set to 7.

Keep in mind that the data step is a pure procedural programming language, where things happen in sequence.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 118

Re: if-else

Its the sequence of the statements.

 

1. First sas read amount=4

2. Next SAS read and executed your first if statement (amount=4).  Given that the condition has been satisfied the assingment statement was executed.

3. Lastly, sas encountered another assignment statement that overwrites the previous value (amount=4) and set it to 7.

Solution
‎09-27-2017 05:49 AM
Super Contributor
Posts: 354

Re: if-else

Hello,

 

amount is first set to 4 with the instruction

 

amount=4;

 

Then  word is set to 'FOUR' with the instruction

 

it amount = 4 then word = ‘FOUR’;

 

lines begining by else can be ignored because the first if condition was met.

 

The last instruction before run sets amount to 7.

 

So in the end amount=7 and word='FOUR' (answer C)

Trusted Advisor
Posts: 1,848

Re: if-else

Put yourself being a computer, do each staement in the order it is written and

fill next table:

         line  staement                                                 amount     word

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

          1    data work.new;                                               .           ' '

          2    length word $7;                                               .           ' '

          3    amount = 4;                                                    4          ' '

          4   it amount = 4 then word = ‘FOUR’;                 4           'Four'

          5   else if amount = 7;                                          4            'Four'            /* no change */

 

Continue to fill it with rest of lines. You will get the answer to your question.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 246 views
  • 2 likes
  • 5 in conversation