I have a macro variable like this :
MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] + CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893]
I have to split it into this form:
VAR1 CHAR6
.
.
.
VAR7 CHARX3
Is this possible?
You don't say anything about what is going on around that or the process, so I just guess you want a dataset out:
%let m=MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] + CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893];
data want;
length str wrd $2000;
str=tranwrd("&m.","MACRO + Intercept [0.1921215597] + ","");
do i=1 to countw(str,"+");
wrd=scan(str,i,"+");
wrd=cat("VAR",strip(put(i,best.))," ",substr(wrd,1,index(wrd,"[")-1));
output;
end;
run;
Which begs the question why that string is in a macro variable in the first place. The place for storing data is in a dataset.
I don't want the Intercept to be one of the macro variables. Nice try though.
The code does not include the intercept, clearly shown by the code line:
str=tranwrd("&m.","MACRO + Intercept [0.1921215597] + ","");
So what does my code not do then?
Ok, I realise now. You hardcoded the intercept co-efficient, very bad practise.
Sorry, is this some kind of joke? You have provided a string and asked to split the string, only including the parts of the string that indicate variables. Thus I removed that part of the string which is not pertinent to the requested output. How is this bad practice? Its simple string manipulation to get the required output.
What is bad practice is putting metadata in a macro variable in the first place and then trying to process it afterwards.
As such good luck with programming, I am out of this discussion.
From where do you get that macro variable in the first place? Data has to be kept in datasets, that is the natural habitat.
Ok, so I created the macro from a variable in a data-set. So, it would be easier if I don't create the macro in the first place? How could I do it if it is a variable in a data-set?
How does the original dataset look like? Please post an example of it in a readily usable form (data step with datalines).
And how do you intend to use the wanted result that you posted in your initial question?
I have shown you above, just remove the macro part, and set your dataset.
I propose this solution:
%let var=MACRO + Intercept [0.1921215597] + CHAR6 [0.3071353419] +
CHAR9 [0.4431909076] + CHAR11 [0.2608270872] + CHAR783 [0.3223002876] + CHAR913 [0.3977991533] + CHAR741 [0.9413739214] + WOE_CHARX3 [0.2782062893];
option symbolgen mprint mlogic;
%macro t;
%do i=1 %to %sysfunc(countw(&var,'+'));
%global var&i ;
%if %sysfunc(substr(%sysfunc(scan(&var,&i,'+')),1,4)) eq CHAR %then
%let var&i=%sysfunc(scan(%sysfunc(scan(&var,&i,'+')),1%str()));
%end;
%mend t;
%t;
%put _all_;
Close but you fail on the last variable which doesn't start with char
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: