Hello All!
I am very confused about a code I am writing. I need to create two variables with conditional processing, so naturally I thought the if/then statement. Once I wrote the code, however, it will accept the first if/then series for BirthQTR, but will not create the second variable with customer age/Promo. I have the code, log, and partial output below, can anyone see what I am doing incorrectly?
Code:
data work.season;
set orion.customer_dim;
BirthQTR= QTR(Customer_BirthDate);
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
and
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else='Other';
keep Customer_FirstName
Customer_LastName
Customer_BirthDate
Promo
Customer_Age
Promo2;
run;
proc print data=work.season;
run;
Log:
57
58 data work.season;
59 set orion.customer_dim;
NOTE: Data file ORION.CUSTOMER_DIM.DATA is in a format that is native to another host, or the file encoding does not match the
session encoding. Cross Environment Data Access will be used, which might require additional CPU resources and might reduce
performance.
60 BirthQTR= QTR(Customer_BirthDate);
61 if BirthQTR =1 then Promo='Winter';
62 else if BirthQTR =2 then Promo='Spring';
63 else if BirthQTR =3 then Promo='Summer';
64 else if BirthQTR =4 then Promo='Fall';
65 and
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
66 if Customer_Age between 18 and 25 then Promo2='YA';
67 else if Customer_Age gt 65 then Promo2='YA';
____
160
ERROR 160-185: No matching IF-THEN clause.
68 else='Other';
69
70 keep Customer_FirstName
71 Customer_LastName
72 Customer_BirthDate
73 Promo
74 Customer_Age
75 Promo2;
76 run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.SEASON may be incomplete. When this step was stopped there were 0 observations and 6 variables.
WARNING: Data set WORK.SEASON was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.07 seconds
77
78 proc print data=work.season;
79 run;
Partial Output:
1 | James | XXXX | 27JUN1978 | 33 | Spring |
---|---|---|---|---|---|
2 | Sandrina | XXXX | 09JUL1983 | 28 | Summer |
3 | Cornelia | XXXX | 27FEB1978 | 33 | Winter |
Hi Orangerunner10,
I am new to the community myself, so please be gentle when I make some slipups.
Solution wise, you might want to try this:
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else Promo2='Other';
That said, a little educational part.
Try to look through the error message you get and notice that the error starts at the 'and' part. If you look at the IF-THEN-ELSE construction, you can understand this error. You are creating two seperate IF-THEN-ELSE sections and tie them together, which is not possible in this way. If you do need it in one piece, please give us some info on what to achieve, because then we would need to use a nested-if construction (which can be a pain if not careful).
The second adjustment I made is at the ELSE part in the end. I do not see the variable you are assigning the value to, so I put it in.
I can't easily check this for you, as I am more on the SAS DI side, but please give it a try.
If correct, please update the ticket as solved.
The log has pointed the errors, as you see, there is only 'if-else if' statement, but no 'and if-else if' statement, so you should delete the 'and' keyword.
Hi Orangerunner10,
I am new to the community myself, so please be gentle when I make some slipups.
Solution wise, you might want to try this:
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else Promo2='Other';
That said, a little educational part.
Try to look through the error message you get and notice that the error starts at the 'and' part. If you look at the IF-THEN-ELSE construction, you can understand this error. You are creating two seperate IF-THEN-ELSE sections and tie them together, which is not possible in this way. If you do need it in one piece, please give us some info on what to achieve, because then we would need to use a nested-if construction (which can be a pain if not careful).
The second adjustment I made is at the ELSE part in the end. I do not see the variable you are assigning the value to, so I put it in.
I can't easily check this for you, as I am more on the SAS DI side, but please give it a try.
If correct, please update the ticket as solved.
You have an error in your program.
else if BirthQTR =4 then Promo='Fall';
and
if Customer_Age between 18
The form of an if statement is:
if <condition> then <statement>;
the format of and if/else is:
if <condition> then <statement>;
else if <conditon> then <statement>;
else <statement>;
You can use do; and end; blocks for multiple statements. It looks like you are trying to nestle if statements:
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
/* The above is an if/else if statement, and BirthQTR gets created based on 1-4, other being missing
and
/* The above is wrong */
/* Here we start a new if then statement */
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else='Other';
Several items to fix ...
1) The word "and" can't just be inserted into the middle of a program. It does have uses here and there, but line 65 should come out entirely. That will also fix the later error.
2) else='Other'; creates a variable named ELSE. Doubtful that's what you are looking for, rather it looks like you want to assign PROMO2 a value of 'Other'. To make that happen, the last line should read: else Promo2='Other';
3) PROMO2 has a length of 2, because the first time it is encountered it is set to 'YA'. That is not long enough to hold 'Other'. So this line should appear before the IF/THEN statements that assign a value to PROMO2: length Promo2 $ 5;
Good luck.
Hi,
Just remove "and" in the code.
else if BirthQTR =4 then Promo='Fall';
and ---------------------- remove this line
if Customer_Age between 18 and 25 then Promo2='YA';
In customer_age condition you gave else = "Other"
For this statement, SAS considers else as a new variable and assigns the value other to else variable.
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else='Other'; *else Promo2="Other";
If you run this code, you will get the Promo2 values as either 'YA' or 'Ot' because while creating the Promo2 variable for the first time, we assigned the value to Promo2 as 'YA' .
SAS takes the default length as 2 for the variable Promo2.
Inorder to avoid this we have to declare the length of the variables explictily.
This is applicable for BirthQtr also. But the initial value for BirthQTR is WINTER.
The length of the remaining values for this variable is less than or equal to 6.
So there wont be any problem with this variable.
Make sure that the Library Orion exist and it has Customer_Dim dataset.
data work.season;
set orion.customer_dim;
length Promo $10 Promo2 $8;
BirthQTR= QTR(Customer_BirthDate);
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
if Customer_Age between 18 and 25 then Promo2='YA';
else if Customer_Age gt 65 then Promo2='YA';
else Promo2='Other';
keep Customer_FirstName
Customer_LastName
Customer_BirthDate
Promo
Customer_Age
Promo2;
run;
proc print data=work.season;
run;
First off, thank you all for the help! I really appreciate it.
I used all of the feedback to help create this final code. I marked Roy's as the most correct, but my program wouldn't accept the code until I repeated "Customer_Age" as indicated in the final code below.
Final Code:
data work.season;
set orion.customer_dim;
BirthQTR = QTR(Customer_BirthDate);
if BirthQTR =1 then Promo='Winter';
else if BirthQTR =2 then Promo='Spring';
else if BirthQTR =3 then Promo='Summer';
else if BirthQTR =4 then Promo='Fall';
if Customer_Age >= 18 and Customer_Age <= 25 then Promo2='YA';
else if Customer_Age >= 65 then Promo2='Senior';
keep Customer_FirstName Customer_LastName Customer_BirthDate
Promo Customer_Age Promo2;
run;
proc print data=work.season;
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.