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?
I also added a statement:
newcolumn=RFFG;
but in log file, im getting this:
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.
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;
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.
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?
You're missing an end:
else if GPP_NOM < 1949 then do;
RFGPP=0.84;
end; /* this was missing */
else RFGPP=0.85;
run;
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;
@Kurt_Bremser: applying maxim 8 will simplify the code ...
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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.