DATA Step, Macro, Functions and more

ERROR in Input

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

ERROR in Input

if  Dept=’Worker’ then input/;

if Dept=’Worker’ then delete;

Input Firstname:$20. Lastname  :20. /

         @cell: ' Cell  $@’email:’ Email  :$60. Major $;

Run;

 

I want to know as per SAS standards does my Input statement has some errors.


Accepted Solutions
Solution
‎11-12-2016 05:50 PM
Super User
Super User
Posts: 6,502

Re: ERROR in Input

[ Edited ]

None of the choices is valid for the code you presented.Since NUMBER is defined in the first step as numeric any attempt to assign strings like 'Unknown' or 'Low' to it will result in missing values. So you will only get missing values.  Now if you were to change the first step so that NUMBER is defined as a character variable (and one that is long enough to store 'Unknown') then you get B.  You will also get a messages about converting the value of NUMBER to an actual number so that the comparisons in the IF statements will run.

 

So let's change the code to something that will run more cleanly. This is probably more like the original problem you wanted to analyze.

data work.test;
  set work.test;
length NumberX $10. ; if number = . then numberX = 'Unknown'; else if number =1 then numberX = 'Low'; else if number=2 or 3 then numberX = 'Medium'; else numberX ='High'; run;

First let's see what answers we can eliminate.

a. Low, Medium, and High only

This cannot be true since the last record in the input clearly has a missing value which will result in UNKNOWN.

d. Low, Medium, High, Unknown and ‘ ‘ (missing character value)

This cannot be true since there is no way to get the value set to blank. If none of the other conditions are true then the last bare ELSE clause will run.

 

So we are left to choose between these two.

b. Low, Medium, and Unknown only
c. Low, Medium, High and Unknown only

The only difference is that the HIGH is not includes in B.  What could happen to prevent that last ELSE from ever executing?  It must be that one of the earlier values is true for every record. Without knowing anything about how SAS processes the conditions you know that it must be the last IF that is at issue. If one of the earlier ones was always true then you wouldn't have values of MEDIUM either.  So let's look at that third IF statement in more detail.

if number=2 or 3 then number = 'Medium';

Both = and OR are binary operators (like + or -) so the order of the operation will make a difference.  If you are not sure which order SAS will do it lets just evaluate both ways and see what would happen.

if number=(2 or 3) then number = 'Medium';

The result of (2 or 3) is 1 (true) since both 2 and 3 are considered true.  Then IF condition will be true only if Number is 1.  But we already eliminated the rows where Number=1 in the previous if statement. So if SAS grouped the operators this way then MEDIUM could never be the answer.  But that is not one of the 4 choices.  

Let's look at the other way of grouping.

if (number=2) or 3 then number = 'Medium';

The result in the () could be 1 or 0 (true or false).  Now both (1 or 3) and (0 or 3) are true since 3 is treated as true. So this is always true. So HIGH can never be the result.  Which means B is a possible right answer. And so SAS must first evaluate the = and then evaluate the OR.

 

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: ERROR in Input

Looks like the website garbled some of your code. Click on the little running man icon and paste the code into the window that pops up.

Contributor
Posts: 34

Re: ERROR in Input

Dear Tom, the bold statement is the one i want to check mistakes, and it is showing exactly what i wrote... Just to pin point my mistakes in that statement.
SAS Super FREQ
Posts: 8,743

Re: ERROR in Input

[ Edited ]

Hi:
Without knowing what your data looks like or how the DEPT variable gets a value (do you have another INPUT statement with a trailing @ sign in your program?), it is hard to comment on your code.

 

And, you did not say whether your SAS log was showing any errors. There are 2 possible types of errors, syntax errors and these will show in the SAS log and logic errors, these will show in your output. For you to ask us to pinpoint errors in a code snippet is nearly impossible to do. Did you have any errors in the SAS log when you ran this code? What makes you think you have mistakes in that statement?

And, with only your code snippet to review, these 2 statements seem contradictory:
if Dept=’Worker’ then input/;
if Dept=’Worker’ then delete;

...where you are asking for both an INPUT and a DELETE action.

Also, I assume that LASTNAME is a character string, not a numeric variable, and you only have 20. for reading LASTNAME instead of $20.

It looks to me like there are some things that need to be fixed and possibly some logic problems to be addressed. But as far as reading 2 INPUT lines and building 1 observation, this example works for me. I had to make some fake data, and if your data does not look like the fake data I created, then your code might need to be different, but since you did not post any data or all of your code, again, it is hard to provide more detailed feedback.

cynthia

read_two_lines.png

Contributor
Posts: 34

Re: ERROR in Input

Thanks for the details. Let me work on my data again and see your inputs.
Contributor
Posts: 34

Re: ERROR in Input

Thanks...I understood ... Very detailed explanation. You are truly a gem of a person.
Super User
Super User
Posts: 6,502

Re: ERROR in Input

[ Edited ]

Hard to tell if it is mistaken without knowing what was intended or needed.First let's make your INPUT statement formatting clearer.

 

input
  Firstname :$20.
  Lastname  :20. 
/ @cell
  : 
  ' Cell  $@’ 
  email :
  ’ Email  :$60. Major $;Run;

This does NOT look valid to me. Let's go through it line by line.

 

Firstname looks fine it will read upto 20 characters stopping at the first delimiter (can't tell what the delimiter is since you didn't provide the INFILE statement).

Lastname looks fine but is seems strange to have a numeric variable called Lastname.

The / says go to a new line.

The @cell says move to the column number in the variable CELL.  Whether it generates an error or not depends on the value of the variable CELL.  It is it missing or < 1 then it will cause an error.  

 

Now things start to look broken .

 

The : modifier needs to be after an input variable name, but you don't have one here since the variable CELL is being used to hold the location that the @ is using to move the column pointer.  So this will definitely cause an error.

Then you have a quoted string.  That is not valid in an INPUT statement unless it preceeded by an @.

Then email : will work. Again the : means to stop at the first delimiter.  If you haven't previously defined a type for EMAIL then it will be defined as a number since you do not provide any format that SAS can use to get a better guess at what type of variable it is.

Then finally you start another invalid string, but this time you don't end it . So the quoted string starts with ’ Email  :$60. Major $; Run;' and will continue until you have closing single quote in your program.  Then SAS will look for the rest of the INPUT statement until it sees a semi-colon.

 

 

Contributor
Posts: 34

Re: ERROR in Input

Thanks... I clearly understood your detailed answer. It's really a great help.... I will request another question , if you have time to explain ... I will post soon.
Contributor
Posts: 34

Re: ERROR in Input

data test;
input obs name $ number;
1 fish 1
2 bear 2
3 panther 2
4 toad 3
5 fox 4
6 eagle .test;
;run;
data work..test;
set work.test;
if number = . then number = 'Unknown';
else if number =1 then number = 'Low';
else if number=2 or 3 then number = 'Medium';
else number ='High';
run;
I have the choice b correct, but i need to know why other choices are not correct. Why not unknown, Low , Medium and High should be result. Need to known the reason why other 3 options are worn.
which of the following values does the variable number contain?
a. Low, Medium, and High only
b. Low, Medium, and Unknown only
c. Low, Medium, High and Unknown only
d. Low, Medium, High, Unknown and ‘ ‘ (missing character value)
SAS Super FREQ
Posts: 8,743

Re: ERROR in Input

Hi:

  What error messages do you see in the LOG when you submit this code? There should be error messages. Unless you have made a typo, there are some thing that will not work and will make it impossible for you to test this question.

 

  Little errors: 

1) On data line 6 is the last value just a . or a .test? If it is .test, then you should be seeing this Note about the data message:

error_with_data_lines.png

2) Did you do a PROC CONTENTS after the first step? If you did, you would see that the variable NUMBER was defined as a numeric variable:

not_change_type.png

Once the variable NUMBER is created as a numeric variable, you CANNOT change the type.

 

3) The DATA statement is wrong...you should NOT have data work..test (2 dots), you should only have data work.test2 or some other name. But even if you correct that, your program will still generate messages:

results_all_missing.png

 

So the fix is to make a new character variable, calling it "NEWNUMBER" or "TYPE" or something else. Or, you could rename the numeric variable NUMBER to something like OLDNUM and make a new CHARACTER variable called NUMBER, but your program is not doing that.

 

4) And, this is an incorrect construction for the ELSE IF:

else if number=2 or 3 then type = 'Medium';

It should be:

else if number in (2, 3) then type='Medium';

or

else if number = 2 or number = 3 then type = 'Medium';

 

so, let's assume you are starting with this data:

work_test_before_new_var.png

 

If you leave the BAD IF statement in the program, then the results will be as shown for TEST3 below: 

bad_if.png

 

 

If you use the "bad" IF with this syntax:

else if number=2 or 3 then type = 'Medium';

 

Then the correct answer would be B because the 'or 3' will treat all the other values of number as "true" or boolean true, so even the number of 4 will get a value of Medium for the TYPE variable.

 

If you correct the bad IF to a good IF, as shown below, then number=4 will get a value of High for the new type variable.

 

good_if.png

 

If you are studying for certification, my suggestion is that you review the Programming 1 and Programming 2 lessons on conditional logic and IF statements.

 

cynthia

 

 

Solution
‎11-12-2016 05:50 PM
Super User
Super User
Posts: 6,502

Re: ERROR in Input

[ Edited ]

None of the choices is valid for the code you presented.Since NUMBER is defined in the first step as numeric any attempt to assign strings like 'Unknown' or 'Low' to it will result in missing values. So you will only get missing values.  Now if you were to change the first step so that NUMBER is defined as a character variable (and one that is long enough to store 'Unknown') then you get B.  You will also get a messages about converting the value of NUMBER to an actual number so that the comparisons in the IF statements will run.

 

So let's change the code to something that will run more cleanly. This is probably more like the original problem you wanted to analyze.

data work.test;
  set work.test;
length NumberX $10. ; if number = . then numberX = 'Unknown'; else if number =1 then numberX = 'Low'; else if number=2 or 3 then numberX = 'Medium'; else numberX ='High'; run;

First let's see what answers we can eliminate.

a. Low, Medium, and High only

This cannot be true since the last record in the input clearly has a missing value which will result in UNKNOWN.

d. Low, Medium, High, Unknown and ‘ ‘ (missing character value)

This cannot be true since there is no way to get the value set to blank. If none of the other conditions are true then the last bare ELSE clause will run.

 

So we are left to choose between these two.

b. Low, Medium, and Unknown only
c. Low, Medium, High and Unknown only

The only difference is that the HIGH is not includes in B.  What could happen to prevent that last ELSE from ever executing?  It must be that one of the earlier values is true for every record. Without knowing anything about how SAS processes the conditions you know that it must be the last IF that is at issue. If one of the earlier ones was always true then you wouldn't have values of MEDIUM either.  So let's look at that third IF statement in more detail.

if number=2 or 3 then number = 'Medium';

Both = and OR are binary operators (like + or -) so the order of the operation will make a difference.  If you are not sure which order SAS will do it lets just evaluate both ways and see what would happen.

if number=(2 or 3) then number = 'Medium';

The result of (2 or 3) is 1 (true) since both 2 and 3 are considered true.  Then IF condition will be true only if Number is 1.  But we already eliminated the rows where Number=1 in the previous if statement. So if SAS grouped the operators this way then MEDIUM could never be the answer.  But that is not one of the 4 choices.  

Let's look at the other way of grouping.

if (number=2) or 3 then number = 'Medium';

The result in the () could be 1 or 0 (true or false).  Now both (1 or 3) and (0 or 3) are true since 3 is treated as true. So this is always true. So HIGH can never be the result.  Which means B is a possible right answer. And so SAS must first evaluate the = and then evaluate the OR.

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 418 views
  • 1 like
  • 3 in conversation