Solved
Contributor
Posts: 61

# nested array to generate a data

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.

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

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

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

## 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: 13,508

## 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: 13,508

## 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: 23,683

## 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 and locked.