BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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.;
5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

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?

ChrisNZ
Tourmaline | Level 20

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,+);
andreas_lds
Jade | Level 19

@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.

 

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 559 views
  • 5 likes
  • 4 in conversation