BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DartRodrigo
Lapis Lazuli | Level 10

Hi guys, 

Today i was trying to do some tricks in SAS.

 

%GLOBAL ACTUAL_MONTH NUM VAR;

DATA EXTRACT;
    LENGTH 	ACTIVITY	$16 F0	3 F1	3 F2	3 F3	3 F4	3 F5	3 F6	3
			F7	3 F8	3 F9	3 F10	3 F11	3 F12	3 F13	3 F14	3
			F15	3 F16	3 F17	3 F18	3 F19	3 F20	3 F21	3 F22	3
			F23	3 F24	3 F25	3 F26	3 F27	3 F28	3 F29	3 F30	3 F31	3;
	INFILE "/Path/SCHEDULER.txt" DLM=";" TRUNCOVER
        DSD ;
	INPUT 	ACTIVITY F0 F1	F2	F3	F4	F5	F6	F7	F8	F9	
			F10	F11	F12	F13	F14	F15	F16	F17	F18	
			F19	F20	F21	F22	F23	F24	F25	F26	F27	
			F28	F29	F30 F31;			
RUN;



%MACRO SCHEDULE(NUM);

%LET ACTUAL_MONTH = %SYSFUNC(DATE(),MONNAME.);
%PUT &ACTUAL_MONTH.;

DATA SCHEDULE;
%DO I = 0 %TO &NUM.;
%LET VAR = %QTRIM(&ACTUAL_MONTH._%SYSFUNC(PUTN(%SYSFUNC(INTNX(DAY,%SYSFUNC(DATE()),+&&I.)),DAY.)));
%PUT &VAR.;
   LENGTH ACTIVITY $ 60;
	SET EXTRACT(RENAME=F&&I=%QTRIM(&&VAR.));
%END;
DROP F:;
RUN;

%MEND;

%SCHEDULE(12);

However code runs well when the values of %schedule are lower or equal than 12, but when i put greater it get an error.

This error occur because the next variable has an space between the name and day.

My question is: how can i strip the column names in the rename or macro creation statements, where i can put values until 31 ?

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try

%QTRIM(&ACTUAL_MONTH._%trim(%SYSFUNC(PUTN(%sysfunc(intnx(day,%SYSFUNC(DATE()),+&&i)),DAY.))));

View solution in original post

3 REPLIES 3
ballardw
Super User

Try

%QTRIM(&ACTUAL_MONTH._%trim(%SYSFUNC(PUTN(%sysfunc(intnx(day,%SYSFUNC(DATE()),+&&i)),DAY.))));

ballardw
Super User

Just a comment on the Rename statement and your NUM parameter. You need a check for month and Num as if Month = February and Num in 30,31 (and usually 29) you will get 1 or more variables mapped to duplicate names and then get an error in rename as F1 and F30 can't map to the same name.

Tom
Super User Tom
Super User

What are you actually trying to do?  

You want to rename F0 to September_17, F1 to September_18.  So what happens when you wrap past the end of the month?

I think you will find the code simpler to maintain if you keep the macro logic and code generation separate.

So here is version that builds the OLD=NEW pairs for the RENAME statement first into a macro variable and then uses that macro in the data step code. Now the data step code itself is not interrupted by the macro logic and is easier to read, understand and make sure you are generating valid SAS code.

 

%macro schedule(num);
%local today month rename ;
%let today=%sysfunc(today());
%let month=%sysfunc(today(),monname);
%do i = 0 %to #
  %let rename=&rename F&i=&month._%sysfunc(intnx(day,&today,&i),day.) ;
%end;

data schedule;
   length activity $60;
   set extract(keep=activity F0-F&num);
   rename &rename;
run;

%mend;

%schedule(12)

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1027 views
  • 1 like
  • 3 in conversation