Hi Everyone,
data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then ;
a = qtr_yr+1;
run;
I wanted to know the conditional processing of IF in the above code the if qtr_yr = 3 is not working properly as I am getting values for "a" for other values of qtr_yr , including where qtr_yr = 3 . On the other hand , in the below code :-
I am getting values for qtr_yr = 3 only .
data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then output ;
run;
Please explain.
In your first program the IF statement is doing nothing because you have no statement between the THEN keyword and the semi-colon that marks the end of the IF statement and no ELSE statement. So when the condition is true it does nothing and when it is false it does nothing.
What is it you are actually trying to do?
Sounds like you want to perform BOTH actions in one step.
data three;
set two;
if qtr_yr = 3 then a = qtr_yr + 1 ;
if qtr_yr = 3 then output ;
run;
You could use a DO/END block to test only once.
data three;
set two;
if qtr_yr = 3 then do;
a = qtr_yr + 1 ;
output ;
end;
run;
Or you could use a subsetting IF instead of and IF/THEN statement.
Notice that a subsetting IF does not use the THEN keyword. You also do not need an explicit OUTPUT statement since only the observations where QTR_YR=3 will make it past the subsetting IF statement.
data three;
set two;
if qtr_yr = 3 ;
a = qtr_yr + 1 ;
run;
Note that the BY statement is doing nothing so I removed it.
Sounds like you may want
data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then a = qtr_yr+1;
run;
The ; in SAS code ends a statement. So your code was unconditionally executing the assignment of A because you ended the If clause too soon.
@Aexor wrote:
Thanks much. How can i remediate the situation ? i am getting confused how if actually processes in SAS . because in the other programmer it worked perfectly.
data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then output ;
run;
Note that in this code you actually have some action taken after the "then". The assignment code did not and I showed how to "remediate" if the goal was to assign values to the variable A only when that condition is met.
If the actual problem is more complex you need to provide actual examples of the values involved and ALL the rules for assignment of values to variable A (or other variable names).
@Aexor wrote:
Thanks! I cant paste the actual data but this is sample data
AccNbr StartDate Amt ;
130 06Jan1999 10
120 06Aug1999 10
130 01Jan1990 10
120 29Mar2005 11
110 01Jan1990 11
100 06Dec1999 11
120 02Apr2013 11
110 01May2003 11
130 01Jul2000 11
I want to get output as for a particular year , particular quarter , sum of total amt.
e.g for year 1999 o/p should be
year id quater sum
1999 130 1 20
1999 100 4 11
and so on
So that is a totally different question than what your original code was trying to do.
What do you mean by SUM? Sum over just that quarter? Over that year up to that quarter? Over all data for ID up to that quarter?
Why does your output include only two of the ID values in the input?
@Aexor wrote:
Thanks much. How can i remediate the situation ? i am getting confused how if actually processes in SAS . because in the other programmer it worked perfectly.
Please show us the code you ran in the "other programmer", and tell us which it is, so we can explain the syntax differences.
The code you posted contains a typo, you need to remove the semi-colon after "then"
- Cheers -
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 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.
Ready to level-up your skills? Choose your own adventure.