Prev: Clever macro to pop the last word off a list and return both the word and shortened list
Next: SAS write to word table question
From: CC on 14 Apr 2010 02:45 Hi Dear all, I have a data set looks like: Name Apple International Business Machines General Electric U.S. Airways I am trying to get the first character for each company name. So, the new variable I am looking for will be like: Name NEWVAR Apple A International Business Machines IBM General Electric GE U.S. Airways USA Could you give me some suggestions on the way I can work on. The problem here is that the number of words in each company is different. Like International Business Machines has three words. General Electric has two words. And. like U.S. Airways has a "." in between. Please let me know if there's anyway to work on it. Thanks in advance!!
From: Patrick on 14 Apr 2010 05:50 Something like this should do: data demo; infile datalines; input; length AbbrString $ 20; i=1; do while(scan(_infile_,i,'. ') ne ''); AbbrString=cats(AbbrString,substr(scan(_infile_,i,'. '),1,1)); i+1; end; put AbbrString=; datalines; Apple International Business Machines General Electric U.S. Airways ; run;
From: CC on 15 Apr 2010 02:14 Hi Patrick, Thank you so much for your help!!! On Apr 14, 2:50 am, Patrick <patrick.mat...(a)gmx.ch> wrote: > Something like this should do: > > data demo; > infile datalines; > input; > length AbbrString $ 20; > i=1; > do while(scan(_infile_,i,'. ') ne ''); > AbbrString=cats(AbbrString,substr(scan(_infile_,i,'. '),1,1)); > i+1; > end; > put AbbrString=; > datalines; > Apple > International Business Machines > General Electric > U.S. Airways > ; > run;
From: Richard A. DeVenezia on 22 Apr 2010 15:41
On Apr 14, 2:45 am, CC <chchanghe...(a)gmail.com> wrote: .... > I am trying to get the first character for each company name. So, the > new variable I am looking for will be like: .... > Could you give me some suggestions on the way I can work on. The > problem here is that the number of words in each company is > different. Like International Business Machines has three words. > General Electric has two words. And. like U.S. Airways has a ".." > in between. > > Please let me know if there's anyway to work on it. Thanks in > advance!! Regular expressions are great tools for manipulating texts. data have; length name $100; input; name = _infile_; datalines; Apple International Business Machines General Electric U.S. Airways run; data want; set have; abbr = prxchange ('s/\b(\w)\w*\b/$1/', -1, trim(name)); abbr = prxchange ('s/\W//', -1, abbr); run; |