BookmarkSubscribeRSS Feed
imdickson
Quartz | Level 8

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?

10 REPLIES 10
imdickson
Quartz | Level 8

I also added a statement:

newcolumn=RFFG;

but in log file, im getting this:

SAS Error.PNG

PGStats
Opal | Level 21

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
imdickson
Quartz | Level 8

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;
PGStats
Opal | Level 21

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
imdickson
Quartz | Level 8

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?

 

Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4127 views
  • 2 likes
  • 5 in conversation