DATA Step, Macro, Functions and more

New data set creation by ID and all data in one row

Reply
Occasional Contributor
Posts: 17

New data set creation by ID and all data in one row

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.

Super User
Posts: 2,049

Re: New data set creation by ID and all data in one row

Posted in reply to librasantosh

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

Super User
Posts: 13,913

Re: New data set creation by ID and all data in one row

Posted in reply to librasantosh

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)?

Super User
Posts: 2,049

Re: New data set creation by ID and all data in one row

Posted in reply to librasantosh

@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;
Occasional Contributor
Posts: 17

Re: New data set creation by ID and all data in one row

Posted in reply to novinosrin
Thank you so much
Super User
Posts: 2,049

Re: New data set creation by ID and all data in one row

[ Edited ]
Posted in reply to librasantosh

@librasantosh is that what you really you wanted?

Ask a Question
Discussion stats
  • 5 replies
  • 134 views
  • 0 likes
  • 3 in conversation