I have the following data set:
Param col2 treat subn2
ka1 0.41 ATES .
ka2 1.00 ATES .
ka1 . BREF 0.64
ka2 . BREF 1.00
The data set has missing values for ka1 and ka2 for subn2. What I want to do is to replace the missing ka1 and ka2 values in subn2 with the values ka1 and ka2 values under col2. I tried the inserted SAS code. I got the following syntax error in the log.
I think that my logic is okay but it may not be. Can someone tell me if my logic seems okay and if so how do I correct the syntax error and if the logic is faulty what would be better?
LOG in
[Data new;
482 set Xall_final10_2;
483 retain temp1 temp2;
484 if ka1 in (col2 ne.) then temp1=ka1;
____
22
76
485 if ka2 in (col2 ne.) then temp2=ka2;
____
22
76
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, iterator, (.
ERROR 76-322: Syntax error, statement will be ignored.]
Data new;
set Xall_final10_2;
retain temp1 temp2;
if ka1 in (col2 ne.) then temp1=ka1;
if ka2 in (col2 ne.) then temp2=ka2;
if ka1 in (subn2=.) then ka1=temp1;
if ka2 in (subn2=.) then ka2=temp2;
run;
The combination of in (subn2 = ) or in (subn2 ne ) is erroneous
What you probably mean to do is:
if param = "ka1" and col2 ne . then temp1 = param; else
if param = "ka2" and col2 ne . then temp2 = param; else
if col2 = . then do;
if param = "ka1" then col2 = temp1;
else col2 = temp2;
run;
The IN operator requires a list of values. No functions, no variable names, no operations, just values:
if numeric something like:
if x in ( 1 2 3).
If the comparison is character the values are in quotes
in name in ('Fred' 'John' 'Mary')
in ( col2 ne .) violates two of the rules, Col2 is apparently a variable and "ne" is a comparison operation.
Please provide data in a useable form, like a data step. You do not show a "missing Ka1" and values of ka1 are not "under col2".
If you want to test if a variable has a missing value easiest is: missing(variablename) which returns a numeric 1/0 for "true" or "false"
The combination of in (subn2 = ) or in (subn2 ne ) is erroneous
What you probably mean to do is:
if param = "ka1" and col2 ne . then temp1 = param; else
if param = "ka2" and col2 ne . then temp2 = param; else
if col2 = . then do;
if param = "ka1" then col2 = temp1;
else col2 = temp2;
run;
Just need define temp1 and temp2 variables as char type by assigning $<length>:
Data new;
set Xall_final10_2;
length temp1 temp2 $3;
retain temp1 temp2;
if param = "ka1" and col2 ne . then temp1 = param; else
if param = "ka2" and col2 ne . then temp2 = param; else
if col2 = . then do;
if param = "ka1" then col2 = temp1;
else col2 = temp2;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.