Date: Tue, 14 Jan 2003 09:38:28 -0500
Reply-To: Ian Whitlock <WHITLOI1@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <WHITLOI1@WESTAT.COM>
Subject: Re: assigning values according to conditions set in a table
Content-Type: text/plain; charset="iso-8859-1"
Ciro,
I changed the form slightly to make the code easier. Here is the model.
data w ;
input id $ size code1 $ code2 $ code3 $ ;
cards ;
a 15 15301 czda 1420
b 30 18700 qbrdsa 1800
;
data c ;
input c1 $ sz1 sz2 c2 $ c3 $ x ;
/* code1 size code2 code3 x */
cards ;
15301 0 15 cz 1420 0.8
15301 0 15 qb 1420 0.7
15301 15 . qb 1420 0.6
18700 0 20 qb 1800 0.9
18700 20 . sa 1800 1.1
;
filename code temp ;
data _null_ ;
set c ;
c1 = quote(trim(c1)) ;
c2 = quote(trim(c2)) ;
c3 = quote(trim(c3)) ;
file code ;
put "if code1 = " c1
" and code3 = " c3
" and index(code2," c2 ")"
/ @5 @
;
if not missing ( sz1 ) then
put "and size > " sz1 @;
if not missing ( sz2 ) then
put "and size <= " sz2 ;
else
put ;
put "then x = " x ";" ;
run ;
data wx ;
set w ;
%inc code / source2 ;
run ;
Left to my own wishes I would probably have used a SELECT structure with
WHEN statements instead of IFs. This would require some extra processing at
_N_=1 and end of file. Otherwise it would be very similar and only a
question of what code you want to end up with.
CALL EXECUTE provide and alternative to %INC. Although an older technique I
see it as a little clearer in this case.
IanWhitlock@westat.com
-----Original Message-----
From: ciro baldi [mailto:baldi@ISTAT.IT]
Sent: Tuesday, January 14, 2003 6:50 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: assigning values according to conditions set in a table
dear listers,
i have to write a program that assign values to a numeric variable in a
main dataset according to some conditions that are available in an
external table (say another dataset).
For example the main dataset is like:
id size code1 code2 code3
a 15 15301 czda 1420
b 30 18700 qbrdsa 1800
..................................
..................................
and the conditions dataset is like this
code1 size code2 code3 x
15301 0-15 cz 1420 0.8
15301 0-15 qb 1420 0.7
15301 > 15 qb 1420 0.6
18700 0-20 qb 1800 0.9
18700 > 20 sa 1800 1.1
.....................................
the program i would like to write
should be like:
data main1;
set main;
if (code1='15301') and (0 < size <= 15) and (code2 contains(*) 'cz') and
(code3='1420') then x=0.8;
if (code1='15301') and (0 < size <= 15) and (code2 contains(*) 'qb') and
(code3='1420') then x=0.7;
if (code1='15301') and ( size > 15) and (code2 contains(*) 'qb') and
(code3='1420') then x=0.6;
if (code1='18700') and (0 < size <= 20) and (code2 contains(*) 'qb') and
(code3='1800') then x=0.9;
if (code1='18700') and (> 20) and (code2 contains(*) 'sa') and
(code3='1800') then x=1.1;
.......
run;
so the risulting dataset main1 should be:
id size code1 code2 code3 x
a 15 15301 czda 1420 0.8
b 30 18700 qbrdsa 1800 1.1
..................................
..................................
i can try to standardize as much as possible the conditions table, but i
will remain with the problem of how to translate it in a sas program.
(*) i do not know, if exist, which sas function works in this case.
any help, information, suggestions and so on would be gratly appreciated
ciro