DATA Step, Macro, Functions and more

How can I create a table

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 97
Accepted Solution

How can I create a table

Hi ,  I have a dataset like following and  I want  the variable yymm  to be in  4 quarters (one year) before  the variable yymm_e so I can  complete my missing data of variable roe.  How can I do ?  thanks a lot.
data have;
informat company $15. roe 4.3 yymm yymm_e yymmn6.;
format yymm yymm_e yymmn6.;
input company$  roe yymm  yymm_e ;
cards;a 0.05 198503 198603
a 0.03 198512 198603
a 0.3  198606 198703
b 0.4  198812 198902;
run;

The table structure I want should be as following . 

擷取.PNG


Accepted Solutions
Solution
‎07-28-2017 04:14 PM
Super User
Posts: 19,814

Re: How can I create a table

Your start data is not the same as your last question. It appears as if for every record, you take the year, ignore the month portion and you create a record for every quarter for that year. This is my assumption of the logic, it does help if you can clearly state that. 

 

Logic:

1. Take year for each record.

2. Loop through and create new quarters for each record

3. Output data.

 

The logic previously should be enough to get you started and the answer is below.

 

Spoiler
data have;
informat company $15. roe 4.3 yymm yymm_e yymmn6.;
format yymm yymm_e yymmn6.;
input company$ roe yymm yymm_e;
cards;
a 0.05 198503 198603
a 0.3 198606 198703
b 0.4 198812 198902
;
run;



data skeleton_all;
set have;
*set date to beginning of year;
start_date=intnx('year', yymm, 0, 'b');

*Loop through to get 4 quarters per year;
do i=0 to 3;
date=intnx('qtr', start_date, i, 'e');
format date yymmn6.;
output; *output a new record;
end;

format start_date yymmn6.;
keep company date yymm yymm_e date;
run;

View solution in original post


All Replies
Frequent Contributor
Posts: 97

Re: How can I create a table

I am waiting on line, pls help me! Thanks a lot.

Solution
‎07-28-2017 04:14 PM
Super User
Posts: 19,814

Re: How can I create a table

Your start data is not the same as your last question. It appears as if for every record, you take the year, ignore the month portion and you create a record for every quarter for that year. This is my assumption of the logic, it does help if you can clearly state that. 

 

Logic:

1. Take year for each record.

2. Loop through and create new quarters for each record

3. Output data.

 

The logic previously should be enough to get you started and the answer is below.

 

Spoiler
data have;
informat company $15. roe 4.3 yymm yymm_e yymmn6.;
format yymm yymm_e yymmn6.;
input company$ roe yymm yymm_e;
cards;
a 0.05 198503 198603
a 0.3 198606 198703
b 0.4 198812 198902
;
run;



data skeleton_all;
set have;
*set date to beginning of year;
start_date=intnx('year', yymm, 0, 'b');

*Loop through to get 4 quarters per year;
do i=0 to 3;
date=intnx('qtr', start_date, i, 'e');
format date yymmn6.;
output; *output a new record;
end;

format start_date yymmn6.;
keep company date yymm yymm_e date;
run;
Frequent Contributor
Posts: 97

Re: How can I create a table

Hi reeza , Thank you ver much . I did it in my last thread as you had taught, but i forgot set start-date. I have another question. In your code the start_time was the following, why can't I write 'start_time=yymm'? 

start_date=intnx('year', yymm, 0, 'b');
Super User
Posts: 19,814

Re: How can I create a table

Because it's not the start date of the year. You need to create a record for every quarter of the year. If you start at the date you have it's sometimes halfway through the year. Again, I'm making assumptions on what you've provided. The logic is ultimately yours.

 


lixuan wrote:

Hi reeza , Thank you ver much . I did it in my last thread as you had taught, but i forgot set start-date. I have another question. In your code the start_time was the following, why can't I write 'start_time=yymm'? 

start_date=intnx('year', yymm, 0, 'b');

 

Frequent Contributor
Posts: 97

Re: How can I create a table

OK, I really appreciate, thank you very much.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 178 views
  • 0 likes
  • 2 in conversation