BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Danglytics
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
KarlK
Fluorite | Level 6

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

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

Karl

View solution in original post

8 REPLIES 8
KarlK
Fluorite | Level 6

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

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

Karl

ArtC
Rhodochrosite | Level 12

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.

Linlin
Lapis Lazuli | Level 10

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

Danglytics
Calcite | Level 5

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.

ArtC
Rhodochrosite | Level 12

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.

Danglytics
Calcite | Level 5

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

ArtC
Rhodochrosite | Level 12

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.

Danglytics
Calcite | Level 5

thank you, worked perfectly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1115 views
  • 3 likes
  • 4 in conversation