Dear
I need to split a macro variable value into two macro variables. For this statement i am expecting aaaa+ for first obs . But i am getting "substrn(aaaa+bbbbb,1,find(aaaa+bbbbb,'+'))". Please suggest.
data one;
input a $10.;
datalines;
aaaa+bbbbb
cccc+ddddd
;
%global treat1 treat2;
proc sql noprint;
select strip(put(count (distinct a), best.)) into: cnt from one;
select distinct a into :treat1 -:treat&cnt. from one
order by a;
quit;
%global treatt11;
%let treatt11=%substrn(&treat1.,1,find(&treat1.,'+'));
%put &treatt11.;
You must be getting error messages with this code.
How about you look at them and take the appropriate action to address the issues they raise?
Hint: The result you want is similar to this:
proc sql noprint;
select count(distinct A) into: cnt from ONE;
select distinct A into :treat1 - from ONE order by A;
quit;
%let treatt11=%scan(&treat1,1,+);
@knveraraju91 wrote:
Dear
I need to split a macro variable value into two macro variables. For this statement i am expecting aaaa+ for first obs . But i am getting "substrn(aaaa+bbbbb,1,find(aaaa+bbbbb,'+'))".
This is a perfect example of the nature of the sas macro language: everything is text. If you want to execute data step functions in macro code, than you have to wrap each function-call in %sysfunc(function(....)). Or use one of the macro-functions as @ChrisNZ suggested.
This construct alone:
proc sql noprint;
select strip(put(count (distinct a), best.)) into: cnt from one;
select distinct a into :treat1 -:treat&cnt. from one
order by a;
quit;
is a clear sign of the abuse of the macro processor for data handling. DATA needs to be kept in DATAsets and manipulated with DATA steps and the other tools of the Base SAS language. The use of such macro list is (with a probability of 99.9 %) a design failure. Forget your unhealthy macro obsession and use the proper tools. See Maxims 11,14, 7,8,9, and 1.
Dynamic code can simply be created out of your dataset with call execute, no macro variable lists necessary. NOT NECESSARY.
data two;
set one;
treat = cats(scan(a,1,'+'),'+');
run;
And then do whatever you need to do out of dataset two.
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!
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.