BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jiangmi
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

7 REPLIES 7
ballardw
Super User

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;

jiangmi
Calcite | Level 5

Hi, ballardw,

You made it look so easy.

I learned sth new.

Thank you.


Joe

ballardw
Super User

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

jiangmi
Calcite | Level 5

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;

ballardw
Super User

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.

Reeza
Super User

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

jiangmi
Calcite | Level 5

Thank you all.

I got it now.

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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