BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Your IF statements don't have the right syntax for the logic you want.

IF <condition> THEN <action>;

You have a condition in your IF statement but no action specified.

Another format of the IF statement is the IF/THEN/DO:

IF <condition> THEN DO;
<actions>
END;

And the other is the IF/THEN/ELSE which isn't relevant to this question but you'll likely want to understand how it works as well.

Subsetting IF documentation.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm

IF/THEN documentation
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1j60arf27ll4nn1ejavv3nby4pa.htm

View solution in original post

13 REPLIES 13
Tom
Super User Tom
Super User

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?

Aexor
Lapis Lazuli | Level 10
i missed some thing the code is like this :
data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then a = qtr_yr + 1 ;
run;

i want to create variable a which will be equal to 4 , as i want output only when the qtr_yr = 3
but it is not working
and when i try the second program

data three;
set two;
by qtr_yr yr;
if qtr_yr = 3 then output ;
run;

i am getting all values where qtr_yr = 3

I wanted to know why the if condition is not working in program one.
Thanks!!
Tom
Super User Tom
Super User

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.

ballardw
Super User

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
Lapis Lazuli | Level 10
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;
Reeza
Super User
Your IF statements don't have the right syntax for the logic you want.

IF <condition> THEN <action>;

You have a condition in your IF statement but no action specified.

Another format of the IF statement is the IF/THEN/DO:

IF <condition> THEN DO;
<actions>
END;

And the other is the IF/THEN/ELSE which isn't relevant to this question but you'll likely want to understand how it works as well.

Subsetting IF documentation.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm

IF/THEN documentation
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1j60arf27ll4nn1ejavv3nby4pa.htm
ballardw
Super User

@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
Lapis Lazuli | Level 10
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
Tom
Super User Tom
Super User

@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
Lapis Lazuli | Level 10
1. The code may looks different but I was checking multiple ways to use if condition . I was trying to subset data based of qtr value , then wanted to sum it by group

2. Sum over just that quarter? Over that year up to that quarter : yes I want to get total value of amount (amt) for a year and quarter
1999 130 1 20
1999 100 4 11
3. I gave example for 1999 year only but I need output table which consist each year along with quarter and sum (total of amt)

Reeza
Super User
It does not make sense to do summations in a data step. You use PROC MEANS and apply filter to either your output or input data set using a WHERE statement. If your dates are SAS dates you can easily filter specific quarters/years/months as necessary, or sum them at various intervals using formats.

Are you familiar with PROC MEANS?
Here's a fully worked example of summing data by year, even when the original data is monthly:
https://gist.github.com/statgeek/0cae5568752959b035516d6ac07a20fb

Kurt_Bremser
Super User

@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.

Oligolas
Barite | Level 11

The code you posted contains a typo, you need to remove the semi-colon after "then"

________________________

- Cheers -

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1165 views
  • 6 likes
  • 6 in conversation