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.
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.
"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.
@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* costrun;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 iswritten 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 SASlog.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.
Sir that could be typo by OP lol
I mean missing semicolon
EDIT Sorry you are right.
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.
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.
Thanks a lot Tom for the answer as well as explaining the question and the errors.
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!
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.
Ready to level-up your skills? Choose your own adventure.