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