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

Hi All,

 

Can anyone please explain the below mentioned code as to why the answer is C. As per my understanding the answer should be B because the automatic conversion will happen. 

 

The following SAS program is submitted:
data work.retail;
cost = `20000';
total= .10* cost
run;
What is the result?
A. The value of the variable TOTAL in the output data set is 2000. No messages are written to the SAS log.
B. The value of the variable TOTAL in the output data set is 2000. A note that conversion has taken place is
written to the SAS log.
C. The value of the variable TOTAL in the output data set is missing. An error message is written to the SAS
log.
D. The variable TOTAL in the output data set has no value. The program fails to execute due to a syntax error.
 
Although i did get answer C when i ran the code but i am just posting here to understand the logic.
Thanks in advance.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Hard to tell since your posted code is missing a semi-colon and has backwards tick in place of one of the single quotes around the string literal.  

If you fix those then it should find that one tenth of 20,000 is 2,000, so the answer is B.

data work.retail;
  cost = '20000';
  total= 0.10* cost ;
run;

For the answer to be C the value of the character variable would need to be something that SAS could not convert automatically.  For example with a comma.

134  data work.retail;
135    cost = '20000';
136    total= 0.10* cost ;
137    put (_all_) (=/);
138  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      136:16

cost=20000
total=2000
NOTE: The data set WORK.RETAIL has 1 observations and 2 variables.


139
140  data work.retail;
141    cost = '20,000';
142    total= 0.10* cost ;
143    put (_all_) (=/);
144  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      142:16
NOTE: Invalid numeric data, cost='20,000' , at line 142 column 16.

cost=20,000
total=.
cost=20,000 total=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 142:14
NOTE: The data set WORK.RETAIL has 1 observations and 2 variables.

 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

"Can anyone please explain the below mentioned code as to why the answer is C"

 

The answer is not C

 

2604 data work.retail;
2605 cost = '20000';
2606 total= .10* cost;
2607 put total= ;
2608 run;

NOTE: Character values have been converted to numeric values at the places given by:
(Line) : (Column).
2606:13
total=2000

 

SAS will auto convert using BESTW informat/format if it can. If it cannot,it will generate missing values in most operation.

 

However, Let's take  a step further to make a note when you play with APP functions. The autoconvert behavior isn't quite the same.  Well APP is out of scope for this thread but good to be aware.

PGStats
Opal | Level 21

@ShwetaMalhotra wrote:

Hi All,

 

Can anyone please explain the below mentioned code as to why the answer is C. As per my understanding the answer should be B because the automatic conversion will happen. 

 

The following SAS program is submitted:
data work.retail;
cost = `20000';
total= .10* cost
run;
What is the result?
A. The value of the variable TOTAL in the output data set is 2000. No messages are written to the SAS log.
B. The value of the variable TOTAL in the output data set is 2000. A note that conversion has taken place is
written to the SAS log.
C. The value of the variable TOTAL in the output data set is missing. An error message is written to the SAS
log.
D. The variable TOTAL in the output data set has no value. The program fails to execute due to a syntax error.
 
Although i did get answer C when i ran the code but i am just posting here to understand the logic.
Thanks in advance.

The answer should be D because of the missing semicolon between cost and run.

PG
novinosrin
Tourmaline | Level 20

Sir that could be typo by OP lol

 

I mean missing semicolon

 

EDIT Sorry you are right.

Astounding
PROC Star

Clearly, the program has been retyped and does not accurately reproduce the original question.  Besides the missing semicolon, this line is also incorrect:

 

cost = `20000';

 

Notice the strange open quote.  So the right answer depends on what belongs in the question.

Tom
Super User Tom
Super User

Hard to tell since your posted code is missing a semi-colon and has backwards tick in place of one of the single quotes around the string literal.  

If you fix those then it should find that one tenth of 20,000 is 2,000, so the answer is B.

data work.retail;
  cost = '20000';
  total= 0.10* cost ;
run;

For the answer to be C the value of the character variable would need to be something that SAS could not convert automatically.  For example with a comma.

134  data work.retail;
135    cost = '20000';
136    total= 0.10* cost ;
137    put (_all_) (=/);
138  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      136:16

cost=20000
total=2000
NOTE: The data set WORK.RETAIL has 1 observations and 2 variables.


139
140  data work.retail;
141    cost = '20,000';
142    total= 0.10* cost ;
143    put (_all_) (=/);
144  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      142:16
NOTE: Invalid numeric data, cost='20,000' , at line 142 column 16.

cost=20,000
total=.
cost=20,000 total=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 142:14
NOTE: The data set WORK.RETAIL has 1 observations and 2 variables.

 

ShwetaMalhotra
Calcite | Level 5

Thanks a lot Tom for the answer as well as explaining the question and the errors.

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
  • 6 replies
  • 2665 views
  • 0 likes
  • 5 in conversation