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.
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)
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.
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.
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)
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.