DATA Step, Macro, Functions and more

Problem in adding new column with if else statement in Stored process

Reply
Frequent Contributor
Posts: 77

Problem in adding new column with if else statement in Stored process

Hi. I am trying to add a new column with if else statement.

 

Here it is:

data platformdata fulldata;
set platformdata2;
RFFG =if System_Total_FG < 1974 then do;
	RFFG=0.8;
end;
else if System_Total_FG < 2011 then do;
	RFFG=0.81;
end;
else if System_Total_FG < 2168 then do;
	RFFG=0.82;
end;
else if System_Total_FG < 2201 then do;
	RFFG=0.83;
end;
else if System_Total_FG < 2293 then do;
	RFFG=0.84;
end;
else EFFG = 0.85;
run;

However, the result will not have a new column called RFFG. First of all, System_Total_FG is in one of the dataset and basically what i want is a new column showing the value based on the if else statement. What am i doing wrong?

Frequent Contributor
Posts: 77

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to imdickson

I also added a statement:

newcolumn=RFFG;

but in log file, im getting this:

SAS Error.PNG

Respected Advisor
Posts: 4,932

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to imdickson

RFFG =if... is a syntax error. The correct syntax is IF condition THEN statement; Take the RFFG = part out. Also the last ELSE statement creates a new variable EFFG, I suspect you meant RFFG.

PG
Frequent Contributor
Posts: 77

Re: Problem in adding new column with if else statement in Stored process

Hi, thx for the guide. I remove the RFFG= before the IF statement and changed from EFFG to RFFG.

However, RFFG is still not showing up. Could you guide me further?

 

My new code:

data platformdata fulldata;
set platformdata2 (drop=System_Total_FG);
newcolumn=RFFG;
if System_Total_FG < 1974 then do;
	RFFG=0.8;
end;
else if System_Total_FG < 2011 then do;
	RFFG=0.81;
end;
else if System_Total_FG < 2168 then do; RFFG=0.82; end; else if System_Total_FG < 2201 then do; RFFG=0.83; end; else if System_Total_FG < 2293 then do; RFFG=0.84; end; else RFFG = 0.85; run;
Respected Advisor
Posts: 4,932

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to imdickson

Correct and simplified code would be:

 

data platformdata fulldata;
set platformdata2;
if System_Total_FG < 1974 then RFFG=0.8;
else if System_Total_FG < 2011 then RFFG=0.81;
else if System_Total_FG < 2168 then RFFG=0.82;
else if System_Total_FG < 2201 then RFFG=0.83;
else if System_Total_FG < 2293 then RFFG=0.84;
else RFFG = 0.85;
drop System_Total_FG;
run;

It will create two identical datasets (platformdata and fulldata) based on input dataset platformdata2. Variable system_total_fg will not be part of the output datasets.

PG
Frequent Contributor
Posts: 77

Re: Problem in adding new column with if else statement in Stored process

Hi There. Thx for the guide. It works. However, i want to add another column with a set of if else logic. I am getting error : No Matching IF ELSE Clause

 

My Code:

data platformdata2;/*fulldata*/
set platformdata2;/*(drop=System_Total_FG)*/
/*newcolumn=RFFG;*/
if System_Total_FG < 1974 then do;
	RFFG=0.8;
end;
else if System_Total_FG < 2011 then do;
	RFFG=0.81;
end;
else if System_Total_FG < 2168 then do;
	RFFG=0.82;
end;
else if System_Total_FG < 2201 then do;
	RFFG=0.83;
end;
else if System_Total_FG < 2293 then do;
	RFFG=0.84;
end;
else RFFG = 0.85;



if GPP_Nom < 1599 then do;
	RFGPP=0.8;
end;
else if GPP_NOM < 1649 then do;
	RFGPP=0.81;
end;
else if GPP_NOM <1799 then do;
	RFGPP=0.82;
end;
else if GPP_NOM < 1849 then do;
	RFGPP=0.83;
end;
else if GPP_NOM < 1949 then do;
	RFGPP=0.84;
else RFGPP=0.85;
run;

 

May i know what is missing?

 

Super User
Posts: 7,854

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to imdickson

You're missing an end:

else if GPP_NOM < 1949 then do;
	RFGPP=0.84;
end; /* this was missing */
else RFGPP=0.85;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,854

Re: Problem in adding new column with if else statement in Stored process

[ Edited ]
Posted in reply to imdickson

PS since you never have more than one statement in your "then" branches, the do/end blocks are not necessary:

data platformdata2;/*fulldata*/
set platformdata2;/*(drop=System_Total_FG)*/

/*newcolumn=RFFG;*/
if System_Total_FG < 1974
then RFFG=0.8;
else if System_Total_FG < 2011
then RFFG=0.81;
else if System_Total_FG < 2168
then RFFG=0.82;
else if System_Total_FG < 2201
then RFFG=0.83;
else if System_Total_FG < 2293
then RFFG=0.84;
else RFFG = 0.85;

if GPP_Nom < 1599
then RFGPP=0.8;
else if GPP_NOM < 1649
then RFGPP=0.81;
else if GPP_NOM <1799
then RFGPP=0.82;
else if GPP_NOM < 1849
then RFGPP=0.83;
else if GPP_NOM < 1949
then RFGPP=0.84;
else RFGPP=0.85;

run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 345

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to KurtBremser

@KurtBremser: applying maxim 8 will simplify the code ...

Super User
Posts: 7,854

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to andreas_lds

andreas_lds wrote:

@KurtBremser: applying maxim 8 will simplify the code ...


Smiley Wink

 

Good catch!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,988

Re: Problem in adding new column with if else statement in Stored process

Posted in reply to KurtBremser

No love for select() clauses anymore?

data platformdata2;
  set platformdata2;
  select(system_total_fg);
    when (< 1974) rffg=0.8;
    when (< 2011) rffg=0.81;
    ...
    otherwise rffg=0.85;
  end;
run;

You could probably do it simpler using format ranges also.

Ask a Question
Discussion stats
  • 10 replies
  • 164 views
  • 2 likes
  • 5 in conversation