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.
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.
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.
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
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.
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:
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:
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:
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:
If you leave the BAD IF statement in the program, then the results will be as shown for TEST3 below:
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.
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
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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: