Hi. I am trying to create a new variable ('vari2) based on responses to another variable. To do this, I was going to use the set statement and keep getting the same error. Code and error are below. Any help is appreciated!
data test;
set test1;
IF vari1=. THEN vari2=.;
ELSE IF vari1=(1 OR 0) THEN vari2=1;
run;
19 data test;
20 set test1;
ERROR: File WORK.test1.DATA does not exist.
You are correct about your data and set questions.
The following isn't valid SAS code, as @Ballardw mentioned earlier.
189 ELSE IF vari1=(1 OR 0) THEN vari2=1;
You can use the following instead:
else if vari IN (0,1) then vari2=1;
Its saying you don't have a test1 dataset created. You may have the names backwards or another error somewhere else in your code.
data test1;
set test;
IF vari1=. THEN vari2=.;
ELSE IF vari1=(1 OR 0) THEN vari2=1;
run;
Thanks. I think there's an issue with my libname statement. I have a dataset named 'test' in the folder 'Data1' and want to first create a permanent library, then create a new variable via above code. Do you know what I'm doing wrong with the below?
Code:
libname anaysis 'X:\Proj Mgmt FOLDER\FINAL DATASET\My Data\SAS data\Data1';
data test;
set analysis.test1;
104 libname anaysis 'X:\Proj Mgmt FOLDER\FINAL DATASET\My Data\SAS
104! data\Data1';
NOTE: Libref ANAYSIS was successfully assigned as follows:
Engine: V9
Physical Name:
X:\Proj Mgmt FOLDER\FINAL DATASET\My Data\SAS
104! data\Data1
105 data test;
106 set analysis.test1;
ERROR: Libname ANALYSIS is not assigned.
ERROR: File WORK.analysis.test1.DATA does not exist.
You changed your spelling. You started with ANAYSIS, then switched to ANALYSIS.
Additionally, you say the data is called test, but you refer to it as Test1
Thanks for catching that. I'm not writing the real names of these datasets because of confidentiality, so I mistyped when I wrote dummy names in the forum.
Is it correct that when using the set statement, set must be the name of the existing dataset and the data step is the name of the new set?
I updated code to reflect this and am not getting any error from the set statement. However, I am getting an error for the new variable.
Code:
data analysis2; /*create new dataset called "analysis2*/
set final322; /*from existing dataset called final322*/
IF vari1=. THEN vari2=.; /*create new variable called "vari2" that has either . or 1*/
ELSE IF vari1=(1 OR 0) THEN vari2=1;
run;
Log:
185 data analysis2;
186 set final322;
188 IF vari1=. THEN vari2=.;
--
180
189 ELSE IF vari1=(1 OR 0) THEN vari2=1;
--
180
ERROR 180-322: Statement is not valid or it is used out of proper order. (vari2 is underlined in the log referencing this error)
You are correct about your data and set questions.
The following isn't valid SAS code, as @Ballardw mentioned earlier.
189 ELSE IF vari1=(1 OR 0) THEN vari2=1;
You can use the following instead:
else if vari IN (0,1) then vari2=1;
Thank you!
If vari1 ever has values other than ., 0, or 1 you may want:
ELSE If vari1 in (1,0) then vari2=1;
The vari1=(1 or 0) will always be true as it will compare the portion 1 or 0 first and since SAS treats 1 as True and 0 as False then (true or false) will always resolve to true.
If vari1 doesn't take on any other values then you only need:
Else Vari2=1;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.