I need generate a data like this:
month | qtr | semi_year | year |
1 | 1 | 1 | 1 |
2 | 1 | 1 | 1 |
3 | 1 | 1 | 1 |
4 | 2 | 1 | 1 |
5 | 2 | 1 | 1 |
6 | 2 | 1 | 1 |
7 | 3 | 2 | 1 |
8 | 3 | 2 | 1 |
9 | 3 | 2 | 1 |
10 | 4 | 2 | 1 |
11 | 4 | 2 | 1 |
12 | 4 | 2 | 1 |
13 | 5 | 3 | 2 |
14 | 5 | 3 | 2 |
15 | 5 | 3 | 2 |
16 | 6 | 3 | 2 |
17 | 6 | 3 | 2 |
18 | 6 | 3 | 2 |
19 | 7 | 4 | 2 |
the total number of 'month' may vary (19 in this case).
the data has a pattern, which assign the 'month' into ordered 'qtr', 'semi_year', and 'year' variables.
I am think a good nested array can take care of it. I don't have it, but I am working on it.
I also welcome other suggestions or better solution.
Thanks.
No arrays needed:
data want;
do month = 1 to 19;
qtr= ceil( month/3) ;
semi_year = ceil(month/6);
year = ceil( month/12);
output;
end;
run;
No arrays needed:
data want;
do month = 1 to 19;
qtr= ceil( month/3) ;
semi_year = ceil(month/6);
year = ceil( month/12);
output;
end;
run;
Hi, ballardw,
You made it look so easy.
I learned sth new.
Thank you.
Joe
When you've been doing this for 30 years you'll recognize some patterns/solutions quickly also.
Hi, I have some follow_up questions.
I am trying to put it in a macro, don't know why it is not working.
Can somebody offer some suggestions?
Thanks.
%macro one;
data want;
%do month = 1 %to 19;
qtr= (ceil( month/3)) ;
semi_year= (ceil(month/6));
year= (ceil( month/12));
output;
%end;
run;
%mend;
%one;
If you put:
options mprint;
before the macro code you should see that you are generating the lines in the %do / %end loop 19 times without change. And probably getting all missing values for output as the dataset variable month is undefined, hence missing, and missing/anything results in missing values.
You don't need %do/%to in a data step you can use the regular ones do/to.
I'm gonna guess the following is what you're after:
%macro one(num_months);
data want;
do month = 1 to &num_months;
qtr= (ceil( month/3)) ;
semi_year= (ceil(month/6));
year= (ceil( month/12));
output;
end;
run;
%mend;
%one(19);
Thank you all.
I got it now.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.