BookmarkSubscribeRSS Feed
jacupp711
Calcite | Level 5

Hello, 

 

I have a raw report that runs every month with several employees and the number of hours they have worked in different categories for the month. Once a month I want to aggregate the previous months reports to see how much they have worked in each category for the year. The raw reports are saved in a folder and I want to set the reports using if then statements. The code I am using is here:

 

data Pilrpt;
    if month(today())=5 then
            set &Apr.;
     else if month(today())=6 then
            set &Apr. &May.;
     else if month(today())=7 then
            set &Apr. &May. &Jun.;
     else if month(today())=8 then
            set &Apr. &May. &Jun. &Jul.;
     else if month(today())=9 then
           set &Apr. &May. &Jun. &Jul. &Aug.;
     else if month(today())=10 then
           set &Apr. &May. &Jun. &Jul. &Aug. &Sep.;
     else if month(today())=11 then
           set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct.;
     else if month(today())=12 then
            set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct. &Nov.;
     else if month(today())=1 then
           set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct. &Nov. &Dec.;
     else if month(today())=2 then
           set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct. &Nov. &Dec. &Jan.;
     else if month(today())=3 then
             set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct. &Nov. &Dec. &Jan. &Feb.;
     else if month(today())=4 then
           set &Apr. &May. &Jun. &Jul. &Aug. &Sep. &Oct. &Nov. &Dec. &Jan. &Feb. &Mar.;
run;

 

When I run the code it errors out and says the reports beyond this month do not exist. Which is true before they aren't yet created but I thought it would find the if statement that was correct and set those reports and not try to pull any others. When I write the code and don't include future months it works. I tried adding a 'do' statement but I got the error that I needed an '=' . Any suggestions on how I can conditionally set tables based on the current month?

 

2 REPLIES 2
ballardw
Super User

1) when you get errors: Copy the Code and all the notes, warnings and error messages generate by that block of code. 2) Paste the copied text into a code box opened on the forum with the </> icon.

Pasting the code into to the code box is important as the message windows on this forum will reformat text and move any of the typical diagnostic characters SAS will provide with some errors.

 

Also, you have 12 macro variables. You need to show what they resolve to. If you run this statement:

%put _user_;

the log will have list of all your currently defined user (those are the ones you create) macro variables and there value. Copy from the log and paste into a separate code box.

 

Future months? You have data from months that have not occurred yet? Sounds fishy to me.

 

If your data has anything resembling an actual date value then it is likely easier to always combine the data you have and then use the Today() value to select the range of dates that you want.

mkeintz
PROC Star

You've revealed a distinction between the compilation and execution stages of many (all?) computer languages.  The compilation stage, in this case, has to prepare executable code that would read data sets that don't yet exist.   Since the compiler doesn't know what variables might exist in those non-existant data sets, it can't prepare the program data vector (i.e. list of available variables) for the data step.  Hence the message you got.

 

You could construct a character variables listing the needed data sets, then write that list to a temporary file, to be %INCLUDEd in the desired data step:

 

filename tmp temp;

data _null_;
  array m_datasets {12} $32 _temporary_ 
        ("&Apr.","&May.","&Jun.","&Jul.","&Aug.","&Sep."
        ,"&Oct.","&Nov.","&Dec.","&Jan.","&Feb.","&Mar."
        );

  fy_begdate = intnx("year.5",today(),0,"beg");
  put fy_begdate=date9.;

  n_m_datasets=intck("month",fy_begdate,today())+1;
  put n_m_datasets=;

  length month_list $400;
  do fm=1 to n_m_datasets;
    month_list=catx(" ",month_list,m_datasets{fm});
  end;

  month_list=catx(' ','set',month_list,';');
  file tmp;
  put month_list;
run;

data pirpt;
  %include tmp / source2;
run;

This way the program doesn't force the compiler to test in advance for non-existent datasets.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 395 views
  • 0 likes
  • 3 in conversation