DATA Step, Macro, Functions and more

variable

Reply
Occasional Contributor
Posts: 12

variable

hi,

 

I have a dataset variable having length 600 like below.

 

                                                                                                  variable


    An essay is generally a piece of writing that gives the authors own argument but the definition is vague overlapping with those of an article a pamphlet and a short story Essays have traditionally been subclassified as formal and informal Formal essays are characterized by serious purpose dignity logical organization length whereas the informal essay is characterized by the personal element selfrevelation individual tastes and experiences confidential manner humor graceful style rambling structure unconventionality or novelty of theme etc Essays can consist of a number of elements including literary criticism political manifestos learned arguments observations of daily life recollections and reflections of the author Almost all modern essays are written in prose but works in verse have been dubbed essays Alexander Popes An Essay on Criticism and An Essay on Man While brevity usually defines an essay voluminous works like John Lockes An Essay Concerning Human Understanding and Thomas Malthuss An Essay on the Principle of Population are counter examples In some countries the United States and Canada essays have become a formal.

 

I need to split the variable values with length 300.

like,

 

              variable1                                                                                                                       variable 2                                       

 

   An essay is generally a piece of writing that gives the authors              confidential manner humor graceful style rambling structure

own argument but the definition is vague overlapping with those       unconventionality or novelty of theme etc Essays can consist of
of an article a pamphlet and a short story Essays have traditionally          a number of elements including literary criticism political
been subclassified as formal and informal Formal essays are                 dubbed essays Alexander Popes An Essay on Criticism
characterized by serious purpose dignity logical organization               like John Lockes An Essay Concerning Human Understanding
length whereas the informal essay is characterized by the personal    Thomas Malthuss An Essay on the Principle of Population are
element selfrevelation individual tastes and experiences                      the United States and Canada essays have become a formal.  

Super User
Posts: 6,936

Re: variable

Your variable1 after the split contains more than 300 characters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,829

Re: variable

I don't see how you're separating the string but assuming it's just 300

 

var1 = SUBSTR (string, 1, 300);

var2 = SUBSTR (string, 300);

Regular Contributor
Posts: 194

Re: variable

If you want to avoid cutting inside of a word, you can proceed as follows :

 

data have;
format text $600.;
text="An essay is generally a piece of writing that gives the authors own argument but the definition is vague overlapping with those of an article a pamphlet and a short story Essays have traditionally been subclassified as formal and informal Formal essays are characterized by serious purpose dignity logical organization length whereas the informal essay is characterized by the personal element selfrevelation individual tastes and experiences confidential manner humor graceful style rambling structure unconventionality or novelty of theme etc Essays can consist of a number of elements including literary criticism political manifestos learned arguments observations of daily life recollections and reflections of the author Almost all modern essays are written in prose but works in verse have been dubbed essays Alexander Popes An Essay on Criticism and An Essay on Man While brevity usually defines an essay voluminous works like John Lockes An Essay Concerning Human Understanding and Thomas Malthuss An Essay on the Principle of Population are counter examples In some countries the United States and Canada essays have become a formal.";
run;

data want (keep=text1 text2);
set have;
format text1 text2 $300.;
text1=prxchange("s/^([\w ]{1,300}) .*$/$1/",-1,text);
text2=substr(text,length(text1)+1);
run;

 

 

Super User
Posts: 9,681

Re: variable

[ Edited ]

There are more than 600.

Sorry. found a problem.

 

 

options noquotelenmax;
data x;
a='
An essay is generally a piece of writing that gives the authors
 own argument but the definition is vague overlapping with those of
  an article a pamphlet and a short story Essays have traditionally 
  been subclassified as formal and informal Formal essays are characterized
   by serious purpose dignity logical organization length whereas the informal 
   essay is characterized by the personal element selfrevelation individual tastes
    and experiences confidential manner humor graceful style rambling structure
     unconventionality or novelty of theme etc Essays can consist of a number 
     of elements including literary criticism political manifestos learned 
     arguments observations of daily life recollections and reflections of 
     the author Almost all modern essays are written in prose but works in 
     verse have been dubbed essays Alexander Popes An Essay on Criticism and
      An Essay on Man While brevity usually defines an essay voluminous works 
      like John Lockes An Essay Concerning Human Understanding and Thomas 
      Malthuss An Essay on the Principle of Population are counter examples 
      In some countries the United States and Canada essays have become a formal.';
run;
data temp;
 set x;
 n+1;
 length temp $ 100;
 do i=1 to countw(a,' ');
  temp=scan(a,i,' ');
  output;
 end;
keep n temp;
run;
data temp;
 set temp;
 by n;
 if first.n then do;len=0;group=0;end;
 len+length(temp)+1;
 if len gt 300 then do;len=length(temp)+1;group+1;end;
run;
data temp1;
length var $ 400;
do until(last.group);
 set temp;
 by n group;
 var=catx(' ',var,temp);
end;
keep n var;
run;
proc transpose data=temp1 out=want(drop=_name_);
by n;
var var;
run;
 
Super User
Posts: 6,936

Re: variable

To cut off at the last blank before the 300th character, do

data test;
variable = 'An essay is generally a piece of writing that gives the authors own argument but the definition is vague overlapping with those of an article a pamphlet and a short story Essays have traditionally been subclassified as formal and informal Formal essays are characterized by serious purpose dignity logical organization length whereas the informal essay is characterized by the personal element selfrevelation individual tastes and experiences confidential manner humor graceful style rambling structure unconventionality or novelty of theme etc Essays can consist of a number of elements including literary criticism political manifestos learned arguments observations of daily life recollections and reflections of the author Almost all modern essays are written in prose but works in verse have been dubbed essays Alexander Popes An Essay on Criticism and An Essay on Man While brevity usually defines an essay voluminous works like John Lockes An Essay Concerning Human Understanding and Thomas Malthuss An Essay on the Principle of Population are counter examples In some countries the United States and Canada essays have become a formal.';
length variable1 variable2 $300;
i = 300;
do until (substr(variable,i,1) = ' ');
  i = i - 1;
end;
variable1 = substr(variable,1,i-1);
variable2 = substr(variable,i+1);
drop i;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 5 replies
  • 258 views
  • 1 like
  • 5 in conversation