BookmarkSubscribeRSS Feed
x3d1m4
Fluorite | Level 6

Hello. I try to calculate in different ways with and without macro. 

 

data train_jedna_zmienna;
set train;
keep &zmienna;
run;
proc means data=train_jedna_zmienna noprint; var _numeric_; output out=basic_statistics mean= p25= p75= qrange= /autoname; run; %let kp25=catx('_',&zmienna,P25); %let kp75=catx('_',&zmienna,P75); %let kqrange=catx('_',&zmienna,QRange); /* 3) calculation*/ data obliczenia; set train_jedna_zmienna; if _n_=1 then set basic_statistics; lower_bound_catx=(catx('_',&zmienna,p25)-1.5*app_income_qrange); lower_bound=(app_income_p25-1.5*app_income_qrange); upper_bound=(app_income_p75+1.5*app_income_qrange); lower_bound_macro=(&kp25-1.5*&kqrange); Upper_bound_macro=(&kp75+1.5*&kqrange); Drop _freq_ _type_; run; data all (keep=full); set sashelp.class; full = catx(',',of name--weight); run;

Sas problem.JPG

Why fucn CATX didint work :(( 

1 REPLY 1
PaigeMiller
Diamond | Level 26

Base SAS functions that are used in a DATA step (like CATX) do not work in macro variables or macros, unless you enclose the function inside %SYSFUNC().

 

But there's no need to enclose CATX inside %SYSFUNC(). This following code concatenates text in macro language and might work, depending on whether macro variable &zmienna contains the name of a single SAS variable (but I suspect it doesn't)

 

%let kp25=&zmienna._p25;

If &zmienna contains the name of multiple data set variables, then the above doesn't work.

 

Similiarly, in data obliczenia; you can't use CATX on macro variables the way you are doing it. CATX works on data step variables or text strings, and the value of &zmienna is likely not a single data step variable, which would result in gibberish. Furthermore, CATX produces a text string, and you don't need a text string in the math to compute lower_bound_catx, you need a variable name.

 

In order to make such code work, you first MUST create working code without macro variables for one or two original variables from train_jedna_zmienna. Note, I said MUST. If you can't get that to work, then the code with macro variables will not work. If you do get that to work, then it's relatively easy to create macro code that works.

 

Normally, when we request users to create code without macro variables for one or two original variables, they never do this. They just continue to push forward doing it with macro variables, and this is simply a stubborn and ineffective way to create macro code, and reinforces bad habits. A word to the wise... do the smart thing, and at the same time improve your ability to write macro code — show us code that works for one or two variables without macros and without macro variables.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 440 views
  • 1 like
  • 2 in conversation