DATA Step, Macro, Functions and more

concatenate a macro string with number

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

concatenate a macro string with number

Hi all

I tried to run the following macro

%macro q;

%do i=1 %to 69;

data verif&i ;

set D&&tabla.&i (obs=1000);

if &campo.&i = '0049';

%end;

run;

%mend q;

but whe the code runs I get the following text in the log

data verif45

set FFFFFF       45

if JJJJJJJ 45 = '0049'

How can I obtain the var FFFFF45 without spaces?. The same for var JJJJJ45.

Thanks in advanced


Accepted Solutions
Solution
‎07-31-2014 09:00 AM
Super User
Posts: 5,094

Re: concatenate a macro string with number

Well, you don't say what the results are at this point.  But I think I can see from the program where you are going wrong.  These references do not properly identify your macro variables:

&tabla.&&i

&campo.&&i

Instead, you should be using:

&&tabla&i

&&campo&i

Notice that the dot in the middle should be removed.  Hope this takes care of it but if not, please describe what the results are.

View solution in original post


All Replies
Super User
Posts: 17,912

Re: concatenate a macro string with number

Change how you're creating the macro variable so it doesn't have trailing spaces.

How are you creating it?

SYMPUTX automatically removes trailing spaces for example.

Contributor
Posts: 68

Re: concatenate a macro string with number

it´s a set of variables and I create them with

proc sql;

select tabla, campo

into :tabla1-:tabla69,

       :campo1-:campo69

from test;

quit;

Super User
Posts: 5,094

Re: concatenate a macro string with number

That SQL step would be the best place to remove extra blanks.  Replace:

select tabla, campo

Try this instead:

select strip(tabla), strip(campo)

There are a few ways to fix the problem later if you have to, but removing the blanks at the source is easy.

Good luck.

Contributor
Posts: 68

Re: concatenate a macro string with number

thanks Astounding but it doesn't work Smiley SadSmiley Sad

Here is the entire code:

proc sql;

select strip(tabla),strip(campo)

into  :tabla1-:tabla69,

       :campo1-:campo69

from test;

quit;

%macro entidad ;

%do i=1 %to 69;

data verif&i ;

set Dim.&tabla.&&i (obs=1000);

if &campo.&&i = '1012';

%end;

run;

%mend entidad;

What am I doing wrong?

Thanks

Solution
‎07-31-2014 09:00 AM
Super User
Posts: 5,094

Re: concatenate a macro string with number

Well, you don't say what the results are at this point.  But I think I can see from the program where you are going wrong.  These references do not properly identify your macro variables:

&tabla.&&i

&campo.&&i

Instead, you should be using:

&&tabla&i

&&campo&i

Notice that the dot in the middle should be removed.  Hope this takes care of it but if not, please describe what the results are.

Super User
Super User
Posts: 7,420

Re: concatenate a macro string with number

And you could do:

proc sql;

     create table LOOP as

     select strip(tabla),strip(campo)

     from test;

quit;

data _null_;

     call execute('data verif'||strip(put(_n_,best.))||';

                              set Dim.'||strip(table)||strip(put(_n_,best.))||' (obs=1000);

                              if '||strip(campo)||strip(put(_n_best.)||' = "1012";

                           run;');

run;

Super Contributor
Posts: 275

Re: concatenate a macro string with number

There are blanks in the macro,try to use:%let tabla=&tabla; to remove tails blanks.

Super User
Posts: 10,538

Re: concatenate a macro string with number

It would help to show how your %tabla and %campo macro variables are created. I suspect a quoting or concatenation issue resulting in trailing blanks.

One fix might be before the data step:

let D&&dtabla.&I = %sysfunc(compress (D&&dtabla.&I); and similar for the Campo variable.

Super User
Posts: 9,687

Re: concatenate a macro string with number

set D&&tabla.%left(&i) (obs=1000);

Contributor
Posts: 40

Re: concatenate a macro string with number

As you are trying to attach string to a Macro, you have to use %left with Macro name. Try what Ksharp as said above and this code below

set Dim.&tabla.(%left&i) (obs=1000);

if &campo.(%left&i) = '1012';

%end;

run;

%mend entidad;

Contributor
Posts: 68

Re: concatenate a macro string with number

Hi all

it seems Astounding's option may work but I haven't access to the libname right now for testing

Thanks for your help

p.d: Works Fine, thanks all for your help Regards

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 465 views
  • 6 likes
  • 8 in conversation