DATA Step, Macro, Functions and more

macro to extract text?

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

macro to extract text?

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.


Accepted Solutions
Solution
‎02-17-2012 02:33 PM
Contributor
Posts: 65

macro to extract text?

Put double quotes around the string macro in the substr() invocation. (Untested).

FILE_PERIOD = SUBSTR("&FILE1",7,10) ;

Karl

View solution in original post


All Replies
Solution
‎02-17-2012 02:33 PM
Contributor
Posts: 65

macro to extract text?

Put double quotes around the string macro in the substr() invocation. (Untested).

FILE_PERIOD = SUBSTR("&FILE1",7,10) ;

Karl

Valued Guide
Posts: 632

Re: macro to extract text?

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.

Super Contributor
Posts: 1,636

macro to extract text?

%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) ;

Contributor
Posts: 66

macro to extract text?

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.

Valued Guide
Posts: 632

Re: macro to extract text?

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.

Contributor
Posts: 66

macro to extract text?

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);

Valued Guide
Posts: 632

macro to extract text?

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.

Contributor
Posts: 66

macro to extract text?

thank you, worked perfectly.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 266 views
  • 3 likes
  • 4 in conversation