extract substring from long string

Reply
Contributor
Posts: 60

extract substring from long string

Hi,

I want to extract a string from a long string. for ex:

str='A&B&C&D&E&F&G&H&I&K&L&M&N&G&';

I want to run a loop to extract the value like below.

when 1st loop is completed my extracted string will be

extrc_str:"A&B&C&D&E&"

when second loop is completed my extracted

extrc_str="F&G&H&I&K&";

and next will take all the rest value if it is less then 5

extrc_str":L&M&N&G&"

I tried to use substr and acan but it's not helping me. let me know your thoughts.

Super User
Super User
Posts: 7,411

Re: extract substring from long string

Hi,

data want;

     str='A&B&C&D&E&F&G&H&I&K&L&M&N&G&';

     loop1=substr(str,1,10);

     loop2=substr(str,11,10;

     loop3=substr(str,22);

run;

I am not seeing why you need loops, and you haven't provided information about "what the problem is".

Contributor
Posts: 60

Re: extract substring from long string

Hi

Thanks for your quick response. I need to generate a report on different variable.I have taken all variable in a string with a delimiter sign. but could not pass due some error.

I want to run a loop to extract the sub string.run the report on those variable again,extract the next piece of string and generate the report.

Please note length of the variable is not fixed. and I believe delimiter I need to use to separate the string. let me know if you have better solution to optimize it.

Message was edited by: Pravin Mishra

Super User
Super User
Posts: 7,411

Re: extract substring from long string

Hi,

Can you provide some actual test data, and required output.  I cannot make out from your text what you are trying to achieve, you have a list of variable names in a field with a delimiter?

data _null_;

     set have;

     i=1;

     do until (scan(str,i,"&")="");

          call execute('proc report data=mydata; columns '||strip(scan(str,i,"&"))||'; run;');

     end;

run;

Contributor
Posts: 60

Re: extract substring from long string

My Apology for not making it clear.I am using below sample code to generate the string.

proc sql noprint;

  select catt('table(',name,') / ',name,

   ', out ,(',name,'),'',name)

    into :select separated by ';'

      from dictionary.columns

        where libname='SASHELP' and

              memname='CLASS'

  ;

proc freq data=table_name noprint;

&select ;

run;

&select holding all the variable name with required format. it giving a memory issue after processing certain variable.

I am thinking to extract the value from the string in part and run my proc freq statement.

Let me know if you any suggestion.

Super User
Super User
Posts: 7,411

Re: extract substring from long string

Yes, well first off you have some errors in that code.  Fixed below.  However I would suggest avoiding the macro variable approach as it quickly becomes unwieldy.  You could do the same as:

data _null_;

     set sashelp.vcolumns (where=(libname="SASHELP" and memname="CLASS"));

     by libname;

     if first.libname then call execute('proc freq data=table_name noprint;');  /* Start the proc */

     /* For each variable create a string containing the text and output it */

     call execute('table('||strip(name)||') /'||strip(name)||', out, ('||strip(name)||'),'||strip(name)||';');

     if last.libname then call execute('run;'); */ end proc */

run;

Fixed:

proc sql noprint;
  select catt('table(',name,') / ',name,
   ', out ,(',name,'),',name)

    into :select separated by ';'

      from dictionary.columns

        where libname='SASHELP' and

              memname='CLASS'

  ;
quit;

%put &select.;

Super User
Posts: 9,682

Re: extract substring from long string

OK. You just need three loop ?

data have;
str='A&B&C&D&E&F&G&H&I&K&L&M&N&G&';
run;
data temp;
 set have;
 group=_n_;
 do i=1 to length(str);
  v=char(str,i);output;
 end;
 drop str;
run;
proc rank data=temp out=temp1 groups=3;
by group;
var i;
ranks r;
run;
data want;
 set temp1;
 by group r;
 length var $ 100;
 retain var;
 var=cats(var,v);
 if last.r then do;output;call missing(var);end;
 keep group var;
run;

Xia Keshan

Contributor
Posts: 60

Re: extract substring from long string

Hi Ksharp,

Loop is not defined neither the length of the variable below is the first part of the code.

proc sql noprint ;

  select cat('table ',strip(name),'/ out=',strip(name))

    into :select_char separated by '; '

    from dictionary.columns

    where libname="WORK" and memname="table_name."  and type="num"

  order by name

  ;

quit;

proc freq data=&dtset noprint compress nlevels ;

&select_char ;

run;

I want to separate &select_char macro variable.so that I can run maximum of table statement in one shot.

Target is to run 50 variables in proc freq statement.It will help me to optimize my code and increase the efficiency

Let me know if you need more info

Super User
Posts: 9,682

Re: extract substring from long string

OK. What information do you need from these proc freq ?

I take 2 as an example .

data have;
 set sashelp.class;
 run;

 proc sql  ;
 create table list as
  select cat('table ',strip(name),'/ out=',strip(name),';')     as list
    from dictionary.columns
    where libname="WORK" and memname="HAVE"  and type="num"
  order by name
  ;
quit;

data list;
 set list;
 if mod(_n_,2)=1 then group+1; /** change 2 into 50 or the number you need**/
run;

data _null_;
 set list;
 by group;
 if first.group     then call execute('proc freq data=have noprint compress nlevels ;');
 call execute(list);
 if last.group then call execute('run;');
run;




Xia Keshan

Ask a Question
Discussion stats
  • 8 replies
  • 615 views
  • 0 likes
  • 3 in conversation