|
That's a good approach, but it does require pre-processing the data. A related solution is to calculate the length and then use the $varying format.
--
Jack Hamilton
jfh@alumni.stanford.org
-----Original Message-----
From: Ya Huang <ya.huang@AMYLIN.COM>
Sent: Sunday, July 27, 2008 9:36 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: [SAS-L] Proc Format Puzzle
You can get the length of the first word easily with scan() and length()
function. After that, you just need to create your own format with
nested format:
data xx;
length x $30;
x='Apple, Orange, Pear, Grape';
output;
x='Orange, Pear, Grape, apple';
output;
x='PineApple, Orange, Pear, Grape';
output;
run;
data xx;
set xx;
lenof1stwd=length(scan(x,1,','));
run;
proc sql noprint;
select quote(x)||'=[$'||trim(put(lenof1stwd,best.-l))||'.]'
into :firstlst separated by ' '
from xx
;
proc format;
value $first &firstlst;
run;
proc print data=xx;
var x;
format x $first.;
run;
Obs x
1 Apple
2 Orange
3 PineApple
The log shows the value of firstlst macro var, which shows how it
works:
"Apple, Orange, Pear, Grape "=[$5.] "Orange, Pear, Grape, apple "=
[$6.] "PineApple, Orange,
Pear, Grape"=[$9.]
On Sun, 27 Jul 2008 23:36:47 -0400, Paul Walker <walker.627@OSU.EDU> wrote:
>I would like to create a format that does the following. It takes a comma
>delimited list of items stored in a dataset variable and only displays the
>first item in the list. For example, suppose the variable containing the
>list was named MyList:
>
>Actual Data:
> MyList
> ------
> Apple, Orange, Pear, Grape
>
>After Applying the Format:
> MyList
> ------
> Apple
>
>How can I create the format that achieves this?
>
>-Paul
|