Hi,
I have a macro to stack 2 files, but i want to do is create a variable with the timestamp of where the file is coming from,
I'm not sure how to just extract the text from the code file name to create the variable FILE_PERIOD.
likely pretty obvious but my code below for file_period does not work..
%MACRO STACK(STACKED,FILE1,FILE2) ;
DATA B.&STACKED ; SET A.&FILE1 A.&FILE2 ;
FILE_PERIOD = SUBSTR(&FILE1,7,10) ;
RUN;
%MEND ;
%STACK(STACKED_2011DEC31,FILE1_2011DEC31,FILE2_2011DEC31) ;
Thanks for your help.
Put double quotes around the string macro in the substr() invocation. (Untested).
FILE_PERIOD = SUBSTR("&FILE1",7,10) ;
Karl
Put double quotes around the string macro in the substr() invocation. (Untested).
FILE_PERIOD = SUBSTR("&FILE1",7,10) ;
Karl
Karl is right in that constant text must be quoted, but you my still need to distinguish between incoming data sets. Consider using the INDSNAME= option on the SET statement. Untested code follows:
%MACRO STACK(STACKED,FILE1,FILE2) ;
DATA B.&STACKED ;
SET A.&FILE1 A.&FILE2 indsname=fred;
FILE_PERIOD = SUBSTR(fred,7,10) ;
RUN;
%MEND stacK;
%STACK(STACKED_2011DEC31,FILE1_2011DEC31,FILE2_2011DEC31) ;
Notice that the variable name FRED is not quoted. I also named the macro on the %MEND - this is just good housekeeping.
%MACRO STACK(STACKED,FILE1,FILE2) ;
DATA &STACKED ;
length FILE_PERIOD $ 9;
SET a.&FILE1 a.&FILE2 ;
FILE_PERIOD =scan("&FILE1",2,'_') ;
RUN;
%MEND ;
%STACK(STACKED_2011DEC31,FILE1_2011DEC31,FILE2_2011DEC31) ;
Am I able to embed a text from a macro into a variable name?
the following code isnt working, but i'd like to create 2 vars in this case:
USE_SMS_CNT_INCOM
USE_MMS_CNT_INCOM
DATA WORK.TEST; SET DATA.TEST;
%MACRO USAGE(TYPE);
IF EVENT_TYPE IN ("&TYPE") AND DIRECTION IN ('INCOM') THEN DO;
USE_"&TYPE"_CNT_INCOM=COUNT;
END;
IF EVENT_TYPE IN ("&TYPE") AND DIRECTION IN ('OUTGO') THEN DO;
USE_"&TYPE"_CNT_OUTGO=COUNT;
END;
%MEND;
%USAGE(SMS);
%USAGE(MMS);
Thanks.
In this case you do not want to quote the macro variable. Think of it this way. The macro variable resolves to the text that you might have typed. The quotes have nothing to do with this process. You would never name a variable
USE_"MMS"_CNT_INCOM=COUNT;
the MMS is the resolved value of the macro variable.
In your original post.
FILE_PERIOD = SUBSTR(&FILE1,7,10) ;
fails because &FILE1 does not resolve to a variable name. Instead it is constant text and therefore must be quoted.
Remember it is all about how the resolved value of the macro variable is to be used.
thanks Art,
when i remove the quotes i'm still getting an error for the pcoess
Apparent symbolic reference TYPE_CNT_INCOM not resolved.
DATA WORK.TEST; SET DATA.TEST;
%MACRO USAGE(TYPE);
IF EVENT_TYPE IN ("&TYPE") AND DIRECTION IN ('INCOM') THEN DO;
USE_&TYPE_CNT_INCOM=COUNT;
END;
IF EVENT_TYPE IN ("&TYPE") AND DIRECTION IN ('OUTGO') THEN DO;
USE_&TYPE_CNT_OUTGO=COUNT;
END;
%MEND;
%USAGE(SMS);
%USAGE(MMS);
Sorry my bad for not paying attention. When appending letters or numbers to macro variable follow the macro variable name with a dot so that the parser knows where the name ends.
USE_&TYPE._CNT_OUTGO=COUNT;
Different potential issue. Be sure that &TYPE does not have any leading, trailing, or embedded blanks.
thank you, worked perfectly.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.