BookmarkSubscribeRSS Feed
swayto
Fluorite | Level 6

Practice Question

data word.new;

length word $7;

amount=4;

If amount=4 then word ='four';

else if amount=7 then word = 'seven';

else word='NONE!!!!';

amount=7;

run;

Answer 

Amount Word 

  7 FOUR

Can somebody please explain the rational behind the answer.

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

There is a concept of PDV in SAS, the Program Data Vector is a logical area of memory that is created during the data step processing. SAS builds a SAS dataset by reading one observation at a time into the PDV and, unless given code to do otherwise, writes the observation to a target dataset.So here in the beginning as per the data step, the amount=4; then the same values is used to fill the value of variable amount=4, then the if/then step is read and since amount=4 when its read the word variable is set to FOUR, then again the amount variable is read again and this time since the value is 7 the amount of 4 is overwritten with 7. And since after this there is no if/then the same values will remain and are output.  

Thanks,
Jag
Astounding
PROC Star
The simple answer: SAS executes statements in the DATA step in order.

First it sets AMOUNT to 4.

Next it calculates WORD.

Next it sets AMOUNT to 7.

Last, it outputs the result.
Reeza
Super User

The code as shown has an error in the IF condition and likely would error out.

 


@swayto wrote:

Practice Question

data word.new;

length word $7;

amount=4;

If amount=4 then word ='four';

else amount=7 then word = 'seven';

else word='NONE!!!!';

amount=7;

run;

Answer 

Amount Word 

  7 FOUR

Can somebody please explain the rational behind the answer.


 

swayto
Fluorite | Level 6

Sorry else if 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 550 views
  • 3 likes
  • 4 in conversation