If-then-else matching issue - if-statement

I am getting an error while running this code
no matching if-then clause
if x=1 then do;
if diff>12 and (8<=p<=12 or 3<=p<5) then g=1;
else if (1<=p<=2) then g=2;
else g=3;
end;
pls help me understand my mistake?

I copied your code and ran the following:
data test;
x = 1;
diff = 14;
p=14;
if x=1 then do;
if diff>12 and (8<=p<=12 or 3<=p<5) then g=1;
else if (1<=p<=2) then g=2;
else g=3;
end;
run;
What seems to be the problem? Make sure x, diff and p are defined.

Related

Expecting an arithemetic operator in SAS?

I got the following error:
I don't understand my error please help.
you need space between do and _i_ (index variable) as shown below. as you have it as do_i_. your warning also gives a clue about this.
data RV2;
retain _seed_ 0;
n=20;
p=0.6;
do _i_ = 1 to 100;
binorm1= ranbin(_seed_,n, p);
output;
end;
drop _seed_ _i_;
run;

Call macro variables multiple times within data step SAS

I'm trying to compare if a number of different variables happen in the order I expect using a macro. My code is:
%macro Order (second,first,var);
data order;
set data;
if &second. > &first. then &var._Correct = 1; else &var._Correct = 0;
if &second. < &first. then &var._Error = 1; else &var._Error = 0;
run;
%mend order;
%order(B,A,AB);
%order(C,B,BC);
I have a lot of other variables to compare. The problem is, when I run the macro the output dataset only has the last pair. In this example, that would be BC. I know I can make multiple output datasets and each one would have the pairs, but then I'd have to rejoin them all together. How can I get one dataset that has all of my &var._Correct and &var._Error pairs?
Your problem is that you're rewriting the data step twice. That's unneeded. Most of the time, macros like this can be lines in a data step not whole data steps.
%macro Order (second,first,var);
if &second. > &first. then &var._Correct = 1; else &var._Correct = 0;
if &second. < &first. then &var._Error = 1; else &var._Error = 0;
%mend order;
data order;
set data;
%order(B,A,AB);
%order(C,B,BC);
run;
Something more like that. I'd note a few minor issues here. What if &second=&first? You want no correct and no error, or is that correct or error?
And an easier way to do this:
%macro Order (second,first,var);
&var._correct = (&second. > &first.); *or GE?;
&var._error = (&second. < &first.); *or LE?; *only one of these two;
%mend order;
That puts the same values into the variable in a lot less code.

SAS FIRST.VARIABLE giving no output

I have some SAS code along the lines of:
DATA MY_SAMPLE;
SET SAMPLE;
BY A;
IF A = 1 THEN B = 1;
ELSE IF A ^= 1 THEN B = 0;
ELSE IF MISSING(A) THEN B = .;
IF FIRST.A;
RUN;
which is returning a set with 0 observations (it shouldn't do this). I have sorted the data by A and tried reading the data into an intermediate dataset before applying the IF FIRST.A but get the same results.
Am I missing something completely obvious? I use the FIRST and LAST all of the time!
Agree with #Robert, the sample code should output records, assuming there are records in your input data and it is sorted.
I would double-check the log from your real program/data, and make sure there are no errors, and that the input dataset has records.
If that doesn't help, I would add some debugging PUT statements, something like below (untested):
DATA MY_SAMPLE;
SET SAMPLE;
BY A;
IF A = 1 THEN B = 1;
ELSE IF A ^= 1 THEN B = 0;
ELSE IF MISSING(A) THEN B = .; *This will never be true ;
put "Before subsetting if " (_n_ A first.A)(=) ;
IF FIRST.A;
put "After subsetting if " (_n_ A first.A)(=) ;
RUN;
As Robert noted, as written your Else if Missing(A) would never be true, because if A is missing the prior Else if A ^= 1 will evaluate to true because SAS uses binary logic (true/false), not trinary logic(true/false/null).
Also I would check for any stray OUTPUT statements in your code.
Checked the log; checked the input; closed MSSQL down; opened it up again and lo and behold, code worked first time. Thanks for the downgrade, but I didn't realize that MSSQL is prone to twitches!

SAS regresses missing values

For some reason when SAS does proportional hazards regression it is including those observations that are specified as . as a group in the results. I suspect it has something to do with how I created my variable (and that SAS thinks my numeric variables are characters) but I can't figure out what I did wrong. I am using SAS 9.4
data final; set final;
if edu_d = 'hs less' then edu_regress = 1;
else if edu_d = 'hs' then edu_regress = 1;
else if edu_d = 'some college' then edu_regress = 2;
else if edu_d = 'college plus' then edu_regress = 3;
else if edu_d = 'missing' then edu_regress=.;
run;
Then I run my regression:
proc phreg data=final;
class edu_regress;
model fuptime*dc(0)=edu_regress/rl;
run;
And the output is as follows:
edu_regress . 1 0.10963 0.12941 0.7177 0.3969 1.116 0.866 1.438
edu_regress 1 1 0.22514 0.10949 4.2278 0.0398 1.252 1.011 1.552
edu_regress 2 1 0.21706 0.11410 3.6190 0.0571 1.242 0.993 1.554
Where . is a category instead of treated as missing.
I'm sure I'm making a rookie mistake but I just can't figure it out.
I would clear your output, and re-run the code, and check the log and output.
As I read the docs, to get missing values treated as a category you would need to have /missing on your CLASS statement, which you do not have in the code shown. Without that, I think missing values should be automatically excluded.
When I run PHREG with a CLASS variable that has missing values, I get a note in the log about observations being deleted due to missing values, and the output shows that the number of observations used is less than the number of observations read.
If SAS thinks edu_regress is character, that's possible if it already was on the dataset as character. This is one reason not to do data x; set x; and instead make a new dataset. You should see notes in the datastep when you run it the way you have now regarding numeric to character conversion, if this is indeed the problem.
Anyway, one way to adjust this is to use CALL MISSING. It sets a variable to missing correctly regardless of the type.
data final;
set final;
if edu_d = 'hs less' then edu_regress = 1;
else if edu_d = 'hs' then edu_regress = 1;
else if edu_d = 'some college' then edu_regress = 2;
else if edu_d = 'college plus' then edu_regress = 3;
else if edu_d = 'missing' then call missing(edu_Regress);
run;

Print subset of matrix

I'm trying to create a code to run a simple perceptron in SAS base.
I'd like to print in each iteration (or store in a table) the result and the target, but I get an error when I try to print y[i,]:
proc iml;
use percept; read all var{x1 X2} into X;
read all var{Y} into Y;
W={0,0}; b=0; k=0; L=nrow(X); eta=.8; o=0;
print w b k L eta;
do step = 1 to 6;
mistakes=0;
do i=1 to L;
o=(X[i, ]*W + b);
if Y[i, ]*o <= 0 then do;
W = W + eta*(Y[i, ]-o)*X[i,]`;
b = b + eta*(Y[i, ]-o)*1;
k=k+1; mistakes=mistakes+1;
print o Y[i, ] W b k mistakes;
end;
end;
end;
I get the error:
Syntax error, expecting one of the following: C, COLNAME, F, FORMAT,
L, LABEL, R,
ROWNAME, ], |). The option or parameter is not recognized and will be ignored.
Do I have any other form to print the target?
Thanks a lot!
Per the documentation on PRINT, you need to do it like this:
print(Y[i,])
This is because they overload the [ ] to indicate formatting, rownames/colnames, etc., which is rather silly (but presumably to imitate some other language?). So you just need to wrap (Y[i,]) like so.
Here's a silly example.
proc iml;
use sashelp.class;
read all var{name,sex} into class;
read all var{height,weight,age} into classN;
y = mean(classN[,2]);
print class;
print (class[1:2,]);
print y (class[1:2,]);
quit;