09-18-2015 03:35 PM - edited 09-18-2015 03:40 PM
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 ?
09-18-2015 04:36 PM
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.
09-18-2015 07:17 PM - edited 09-18-2015 07:19 PM
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)
Need further help from the community? Please ask a new question.