Date: Thu, 23 Sep 2010 09:27:50 -0500
Reply-To: "Data _null_;" <iebupdte@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Data _null_;" <iebupdte@GMAIL.COM>
Subject: Re: Conditional find and replace in a text string
In-Reply-To: <410743.29568.qm@web57002.mail.re3.yahoo.com>
Content-Type: text/plain; charset=ISO-8859-1
You could use TRANSLATE funciton and skip the format and put function.
if length(text) eq 15 then do _N_=3,5,9,11,13;
substr(text,_n_,1) = translate(substr(text,_n_,1),'123','321');
end;
You can even translate 2 to 2 as I did here to make it clear you are
reversing the scale.
Sort of a "six of one half dozen" kind of thing.
On Thu, Sep 23, 2010 at 8:46 AM, Paul Miller <pjmiller_57@yahoo.com> wrote:
> Hi Søren and Nat,
>
> Thanks for your replies to my post. Søren, your solution works particularly well and I was hoping you could help me understand a little better how it works.
>
> In your code you have the expression: "do _N_=3,5,9,11,13."
>
> I understand that _N_ is an automatic variable that counts the current interation within the data step. I've seen this used before to count the number of rows but I've never seen it used the way you have here.
>
> Can you give me a better sense of exactly how this works?
>
> Thanks,
>
> Paul
>
> --- On Thu, 9/23/10, Søren Lassen <s.lassen@POST.TELE.DK> wrote:
>
>
> From: Søren Lassen <s.lassen@POST.TELE.DK>
> Subject: Re: Conditional find and replace in a text string
> To: SAS-L@LISTSERV.UGA.EDU, "Paul Miller" <pjmiller_57@YAHOO.COM>
> Received: Thursday, September 23, 2010, 12:21 AM
>
>
> Paul,
> This is a good place to use assignments to substrings:
> data have;
> input text $50.;
> cards;
> 1 1 8 25 0
> 0
> 2 1
> 1 1 1 2 3 2 3 5
> 2 3 3 3 1 1 2 4
> 2 0
> ;
> run;
>
> proc format;
> value $swap
> '1'='3'
> '3'='1'
> ;
> run;
>
> data want;
> set have;
> if length(text)=15 then do _N_=3,5,9,11,13;
> substr(text,_N_,1)=put(substr(text,_N_,1),$swap.);
> end;
> run;
>
> I calculated the positions from the word numbers, as the words
> appear to be in fixed positions. If that is not the case, you can do:
>
> data want;
> set have;
> if length(text)=15 then do _N_=2,3,5,6,7;
> call scan(text,_N_,_P,_L,' ');
> substr(text,_P,_L)=put(substr(text,_P,_L),$swap.);
> end;
> drop _:;
> run;
>
>
> Regards,
> Søren
>
> On Wed, 22 Sep 2010 16:04:58 -0700, Paul Miller <pjmiller_57@YAHOO.COM>
> wrote:
>
>>Hello All,
>>?
>>Below is snippet from a design file for a conjoint analysis.
>>?
>>data test;
>>input text $50.;
>>cards;
>>1 1 8 25 0
>>0
>>2 1
>>1 1 1 2 3 2 3 5
>>2 3 3 3 1 1 2 4
>>2 0
>>;
>>run;
>>?
>>I'd like to do a find and replace that changes some of the values in lines
> with length = 15 (lines 4 and 5 in this example) according to the following
> set of criteria:
>>?
>>position???? action
>>1????????????? do nothing
>>2????????????? 1=3,2=2,3=1
>>3????????????? 1=3,2=2,3=1
>>4????????????? do nothing
>>5????????????? 1=3,2=2,3=1
>>6????????????? 1=3,2=2,3=1
>>7????????????? 1=3,2=2,3=1
>>8????????????? do nothing
>>?
>>where position is the location of the number within the text string.
>>?
>>Does anyone now a good way of doing this?
>>?
>>Thanks,
>>?
>>Paul
>
>
>
>
|