Date: Thu, 19 Apr 2007 15:22:31 +0200
Reply-To: SAS-L List <sas-l@listserv.uga.edu>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Robert Bardos <bardos2@ANSYS.CH>
Subject: Re: Problem with conditional if-else
In-Reply-To: <I6idna3P_8jx97rbRVnzvQ@telenor.com>
Content-Type: text/plain; charset="iso-8859-1"
Rune,
syntactically you are testing for variables named 'yes' or 'no'
and not for character strings. So you could try
if "&doklabel" eq "yes" then ....
Genererally be careful with this type of solution. It is very easy
to have something like
%let doklabel = Yes ;
which would call for either %lowcase or %upcase for enhanced
stability.
Kind regards
Robert
> -----Ursprüngliche Nachricht-----
> Von: SAS(r) Discussion
> [mailto:SAS-L@LISTSERV.UGA.EDU]Im Auftrag von
> Rune Runnestø
> Gesendet: Donnerstag, 19. April 2007 15:11
> An: SAS-L@LISTSERV.UGA.EDU
> Betreff: Problem with conditional if-else
>
>
> Here is an example where a conditional logic is not
> doing what I thought it
> would do. The code can be copied and pasted into the
> editor and submitted as
> soon as you have adjusted the filename statement.
>
> *creating a file on disk;
> filename myfile 'd:\temp\myfile.txt';
> data _null_;
> infile datalines;
> file myfile;
> input;
> put _infile_;
> datalines;
> SAK NR.: 1996/00009 - 1
> SAK NR.: 1996/00009 - 2
> SAK NR.: 1996/00009 - 3
> SAK NR.: 1996/00009 - 4
> SAK NR.: 1996/00009 - 5
> run;
>
> /*
> Description of the problem:
> Sometimes, as in this case, the record is like this:
> SAK NR.: 1996/00009 - 1
> Othe times, the record looks like this:
> SAK NR.: 1996/00009 DOKNR.: 1
>
> The macrovariable below tells whether the label DOKNR.:
> is represented in
> the file (YES) or not (NO).
> In this case, it is not represented. Insted, the
> representation is '-'.
> */
> *%let doklabel = yes;
> %let doklabel = no;
> data myds (drop = yes no);
> length
> Saksnr $10.
> Doknr 3.
> tmp_FullLine $500.
> tmp_position 8.
> tmp_piece $70.
> ;
> *reading the file from disk;
> infile myfile dlm='~' pad ;
> input @01 @"SAK NR.:" @01 tmp_FullLine ;
> tmp_position = index(tmp_FullLine,"SAK NR.:");
> tmp_piece = scan(substr(tmp_FullLine,tmp_position+length("SAK
> NR.:")-1,19),2,' ');
> if tmp_position ne 0 and tmp_piece ne '' then saksnr
> = tmp_piece;
> else saksnr = .;
>
> *this if-clause is not supposed to run;
> if &doklabel = yes then do;
> tmp_position = index(tmp_FullLine,"DOKNR.:");
> tmp_piece =
> scan(substr(tmp_FullLine,tmp_position+length("DOKNR.:")-
> 1,14),2,' ');
> if tmp_position ne 0 and tmp_piece ne '' then doknr =
> input(tmp_piece,3.);
> else doknr = .;
> end;
> *but this is supposed to run;
> else if &doklabel = no then do;
> *Problem: Why is tmp_position always set to 0 ? It
> is supposed to be 28,
> which is the
> position of '-'. ;
> tmp_position = index(tmp_FullLine,'-');
> tmp_piece =
> scan(substr(tmp_FullLine,tmp_position+length('-')-1,14),2,'
> ');
> if tmp_position ne 0 and tmp_piece ne '' then doknr =
> input(tmp_piece,3.);
> else doknr = .; /* this should never be true in
> this case, but it
> always is !?? */
> end;
> run;
>
>
> Is there anyone out there who can crack this nut (or
> may be all of you are
> busy on SAS Global Forum ) ?
>
> Regards, Rune
>
|