DATA Step, Macro, Functions and more

nested array to generate a data

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

nested array to generate a data

I need generate a data like this:

monthqtrsemi_yearyear
1111
2111
3111
4211
5211
6211
7321
8321
9321
10421
11421
12421
13532
14532
15532
16632
17632
18632
19742

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.


Accepted Solutions
Solution
‎10-24-2014 03:57 PM
Super User
Posts: 10,516

Re: nested array to generate a data

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;

View solution in original post


All Replies
Solution
‎10-24-2014 03:57 PM
Super User
Posts: 10,516

Re: nested array to generate a data

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;

Contributor
Posts: 61

Re: nested array to generate a data

Hi, ballardw,

You made it look so easy.

I learned sth new.

Thank you.


Joe

Super User
Posts: 10,516

Re: nested array to generate a data

When you've been doing this for 30 years you'll recognize some patterns/solutions quickly also.

Contributor
Posts: 61

Re: nested array to generate a data

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;

Super User
Posts: 10,516

Re: nested array to generate a data

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.

Super User
Posts: 17,867

Re: nested array to generate a data

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

Contributor
Posts: 61

Re: nested array to generate a data

Thank you all.

I got it now.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 229 views
  • 3 likes
  • 3 in conversation