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 (April 2007, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 19 Apr 2007 15:11:00 +0200
Reply-To:     Rune RunnestÝ <rune@FASTLANE.NO>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Rune RunnestÝ <rune@FASTLANE.NO>
Subject:      Problem with conditional if-else
Comments: To:

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

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