lord12
lord12

Reputation: 2927

How to combine text and numbers in catx statement

The variable upc is already defined in my cool dataset. How do I convert it to a macro variable? I am trying to combine both text and numbers. For example blah should equal upc=123;

    data cool;
    set cool;

    blah = catx("","upc=&upc","ccc")
    run;

Upvotes: 0

Views: 1264

Answers (2)

Dmitry Shopin
Dmitry Shopin

Reputation: 1763

If upc is a numeric variable and you just want to include its value into some character string then you don't need to do anything special. Concatenation function will convert it into character before concatenating automatically:

data cool;
    blah = catx("","upc=",upc,"ccc");
run;

The result:

upc----blah
123    upc= 123ccc

BTW, if you want to concatenate strings without blanks between them, you can use function CATS(), which strips all leading and trailing spaces from each argument.

Upvotes: 1

Bill Huang
Bill Huang

Reputation: 4648

The following test code works for my SAS 9.3 x64 PC. Please note that:

1.symputx() provide the connection between dataset and macro variables.

2.cats() will be more appropriate than catx() if delimiting characters are not needed.

3.If you did not attempt to create a new data set, data _NULL_ is fine.

You can check the log to see that the correct values are being assigned.

Bill

data a;
    input ID $ x y @@;
    datalines;
A 1 10 A 2 20 A 3 30
;
run;

options SymbolGen MPrint MLogic MExecNote MCompileNote=all;

data _NULL_;
    set a;
    call symputx(cats("blah",_N_),cats(ID,x),"G");
run;
%put blah1=&blah1;
%put blah2=&blah2;
%put blah3=&blah3;

Upvotes: 0

Related Questions