BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pchen002
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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

4 REPLIES 4
Kurt_Bremser
Super User

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.

ShiroAmada
Lapis Lazuli | Level 10

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.

gamotte
Rhodochrosite | Level 12

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)

Shmuel
Garnet | Level 18

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2462 views
  • 4 likes
  • 5 in conversation