LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (July 2008, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 28 Jul 2008 05:59:39 -0700
Reply-To:   Jack Hamilton <jfh@STANFORDALUMNI.ORG>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Jack Hamilton <jfh@STANFORDALUMNI.ORG>
Subject:   Re: Proc Format Puzzle
Comments:   To: Ya Huang <ya.huang@AMYLIN.COM>
Content-Type:   text/plain; charset="iso-8859-1"

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


Back to: Top of message | Previous page | Main SAS-L page