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

Hi can someone help me how to write excel formula in SAS

 

e.g., below formula i need to be implemented in SAS

=IF(G8<3,IFERROR(INDEX($B$1:$F$1,MATCH(3,B8:F8,0)),IF(G8<2,IFERROR(INDEX($B$1:$D$1,MATCH(2,B8:E8,0)),""),"")),"")

 

1 ACCEPTED SOLUTION

Accepted Solutions
kishoresamineni
Quartz | Level 8
data R_MONTH;
SET r_t_format_month;
array mth(6) May June July August September October;
if mth(6) = 3 then
Month = " ";
else if mth(6) = 2 then
do;
do i = 1 to 5;
if mth(i) = 3 then 
do; Month = i; leave; end;
end;
end;
else;
if mth(6) = 1 then
do;
do i = 1 to 5;
if mth(i) = 3 then
do;
Month = i; leave; end;
end;
if Month = " " then
do;
do i = 1 to 5;
if mth(i) = 2 then
do;
Month = i; leave; end;
end;
end;
end;
if Month NE " " then
NEW_MONTH = vname(mth(Month));
run;

View solution in original post

45 REPLIES 45
PeterClemmensen
Tourmaline | Level 20

@kishoresamineni no doubt that the overall logic can be implemented in SAS. Seems like some kind of lookup. 

 

However, Excel and SAS are widely different. Thus, you should describe your problem in more detail and not just with an Excel formula you want translated 🙂

kishoresamineni
Quartz | Level 8

Hi draycut,

 

actually i looking for a month name to populate if last six month are in stage 2 0r 3

e.g. 3 3 3 2 2 2 then Month Name should be May

1 1 2 2 1 1 then Month Name should be July

2 2 2 2 2 2 then Month should be Blank

3 3 3 3 3 3 then Month should be Blank

2 2 2 1 1 1 then Month should be Blank

3 2 2 2 2 2 then Month should be May

1 1 1 1 2 2 then Month should be Blank

 

formula what i have in excel does above conditions, now i need similar conditions to be implemented in SAS

PeterClemmensen
Tourmaline | Level 20

What is the logic here? Why is month name='July' in the second obs but blank in the third?

kishoresamineni
Quartz | Level 8

we see the data for last six months only,

if everything is in stage 2 then it should be blank

similarly if every month is in stage 3 then it should be blank

PeterClemmensen
Tourmaline | Level 20

Doesn't answer why 2 2 2 1 1 1 yields a blank? Or how the months are determined when they should not be blank?

kishoresamineni
Quartz | Level 8

data temp(drop= p_id p_stage); /*temp*/
set month_fmt;
by id;
p_id = lag(id);
p_stage = lag(stage);
if p_id = id then do;
if p_stage < stage then output;
end;
run;

proc sql;
create table temp1 as /*temp1*/
select id, max(stage) as stage,
max(date) as Month format date9d. from temp
group by id;
quit;

proc transpose data=month_fmt out=input1(drop= _name_); /*input1*/
by id;
var Stage;
id date;
run;

proc sql;
create table input2 as select a.*,b.Month as Month /*input2*/
format = MONNAME9. from input1 as a left join temp1 as b
on a.id=b.id;
quit;

 

this is the programs that i have used, i am getting the Month names but not meeting all the conditions

e.g., 1 1 1 1 2 2 should be blank but i am getting Month as September

2 3 3 3 3 3 should be blank but i am getting Month as June

3 3 3 1 2 2 should be May but i am getting September

 

please help me in solving the above issues.

kishoresamineni
Quartz | Level 8

i have date column in my input data

kishoresamineni
Quartz | Level 8

in my input data i have DATE column as:

31MAY2019

30JUN2019

31JUL2019

31AUG2019

30SEP2019

31OCT2019

 

these dates will be there for every ID

kishoresamineni
Quartz | Level 8

all the six dates should be considered, because i want the report for last 6 months of data

and every account has six months data.

Kurt_Bremser
Super User

@kishoresamineni wrote:

all the six dates should be considered, because i want the report for last 6 months of data

and every account has six months data.


That would mean that your target variable would contain the string "MayAprilJuneJulyAugustSeptember", but it only contains ONE month name.

HOW DO YOU DETERMINE THIS MONTH NAME?

kishoresamineni
Quartz | Level 8

it contains data from May - Oct

and Month name should be picked based on the stage. 

My program worked fine for picking the Name name in Month column based on the stage ID was in.

But, some of the Month's conditions are not correct, so i have pasted the excel formula used.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 45 replies
  • 2244 views
  • 5 likes
  • 5 in conversation