BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lixuan
Obsidian | Level 7
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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
lixuan
Obsidian | Level 7

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

Reeza
Super User

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;
lixuan
Obsidian | Level 7

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');
Reeza
Super User

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');

 

lixuan
Obsidian | Level 7

OK, I really appreciate, thank you very much.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1204 views
  • 0 likes
  • 2 in conversation