BookmarkSubscribeRSS Feed
librasantosh
Obsidian | Level 7

Hi attached is dataset sample. I want to get dataset for id 1 have all info in one row. Also want to create variable how much expenses for id 1 in last 3 month for expense _type,   3m_a_avg_amt.

5 REPLIES 5
novinosrin
Tourmaline | Level 20

sounds like a simple transpose after where id=1 and loop through for the last 3 months.

 

What should your result look like? There is no output sample

ballardw
Super User

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Do you want a data set (input to other processes) or a report (people will read)?

novinosrin
Tourmaline | Level 20

@librasantosh Until you clarify , some fun:

 


data have;
input id	Expense_day : date7.	Expens_type $	Expense;
format  Expense_day date7.;
datalines;
1	7-May-05	a	4.2
1	19-May-05	a	4.7
1	20-May-05	a	3.1
1	22-May-05	a	4.1
1	16-Jun-05	a	6.5
1	22-Jun-05	a	3.8
1	30-Jun-05	a	4.8
1	16-Jul-05	a	4.4
1	24-Jul-05	a	1.1
1	26-Jul-05	b	6.6
1	7-Aug-05	b	2.6
1	20-Aug-05	b	1.2
2	23-Aug-05	a	2.4
2	26-Sep-05	a	4.8
2	22-Oct-05	a	1.3
2	24-Oct-05	a	1.5
2	6-Nov-05	a	27.4
2	10-Nov-05	b	4.9
2	14-Nov-05	b	1.4
2	26-Nov-05	b	1.7
;
data want;
do until(last.id);
set have;
by id;
_month=month(Expense_day);
end;
call missing(_t);
do until(last.id);
set have;
by id;
if month(Expense_day)>=_month-3+1 then _t+Expense;
end;
mon3_a_avg_amt=divide(_t,3);
drop _:;
run;
librasantosh
Obsidian | Level 7
Thank you so much
novinosrin
Tourmaline | Level 20

@librasantosh is that what you really you wanted?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1440 views
  • 0 likes
  • 3 in conversation