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.

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
  • 4 replies
  • 1619 views
  • 4 likes
  • 5 in conversation