/****** DO LOOPs***** DO index-var=start TO stop BY increment SAS statements END; */ /* Compare CD investment Tab delimited data DLM='09'x */ data compare; investment = 5000; input bank & $17. rate 8.4 years; do i=1 to years; investment + rate*investment; end; drop i; datalines; MBNA AMERICA 0.0817 5 Metropolitan Bank 0.0814 3 Standard Pacific 0.0806 4 ; proc print data = compare;run; data invest; capital=2000; do until (capital ge 20000); capital=capital + capital*0.05; *output; end; run; proc print data = invest;run; data consump; *infile datalines /*dsd dlm='09'x */; input YEAR @; input USCNSMP @;output; input USCNSMP @;output; input USCNSMP @;output; input USCNSMP @;output; datalines; 1955 248.7 253.7 259.9 261.8 1956 263.2 263.7 263.4 266.9 1957 268.9 270.4 273.4 272.1 1958 268.9 270.9 274.4 278.7 1959 283.8 289.7 290.8 292.8 1960 295.4 299.5 298.6 299.6 1961 297.0 301.6 . . ; run; proc print data = consump;run; data consump; *infile datalines /*dsd dlm='09'x */; input YEAR @; do QUART = 1 to 4; input USCNSMP @; output; end; datalines; 1955 248.7 253.7 259.9 261.8 1956 263.2 263.7 263.4 266.9 1957 268.9 270.4 273.4 272.1 1958 268.9 270.9 274.4 278.7 1959 283.8 289.7 290.8 292.8 1960 295.4 299.5 298.6 299.6 1961 297.0 301.6 . . ; run; proc print data = consump;run; /* Reading VARYINg number of repeating Fields Create a counter variable */ data consump; infile datalines MISSOVER; input YEAR USCNSMP : 5.1 @; rec=0; do while (USCNSMP ne .); rec +1; output; input USCNSMP : 5.1 @; end; datalines; 1955 248.7 253.7 259.9 261.8 1956 263.2 263.7 263.4 266.9 283.3 1957 268.9 270.4 273.4 272.1 298.6 278.5 1958 268.9 270.9 274.4 278.7 1959 283.8 289.7 290.8 292.8 279.3 1960 295.4 299.5 298.6 299.6 1961 297.0 301.6 ; run; proc print data = consump;run; /* */ /* */ /* read across with @@ */ /* Need to hold current record until each block of data has been read and written to the dataset as an observation---- trailing @ ---- holds the input record for the execution of the INPUT stmt Releases a record when control returns to the top of the data step Double trailing @@ holds the input record for the execution across iterations of data step data _N_ Score @@ _N_ score 102 92 78 103 84 23 36 75 2 84 2 92 @@ --likes @, but except holds the dataline in the input beffer across multiple executions of data step typically used to read multiple obs from a single line shouldn't be used with - @ pointer ocntrol, column input and MISSOVER option */ data across ; input y @@ ; * @@ says hold record, position ; put 'after input ' y= _n_= ; * what do we have? ; cards ; 1 2 3 4 5 ; run ; proc print data=across ; title 'read across with @@'; run; /* */ /* */ /* read across with @@ and do blocks */ data inarow ; input y @@ ; * @@ says hold record, position ; put 'after input ' y= _n_= ; * what do we have? ; if ( y > 3 ) then do ; * begins block of stmts ; z = y*y ; output; end ; * ends block of stmts ; else do ; z = 1 ; put 'y is too big' ; end; cards ; 1 2 3 4 5 ; run ; proc print data=inarow ; title 'Example Using IF-THEN-ELSE and Do-End block'; run; /* */ /* */ data two ; input treat $ @@ ; * how long? ; do subject = 1 to 4 ; * begin do loop ; input yield @@ ; output ; * what if I forgot this? ; end; * loop on subject ; put 'after loop ' _all_ ; * peek at PDV here ; cards; treatment 30 31 28 29 control 29 28 27 25 ; run; /* what do we get? */ proc print data=two; title 'Read in a row with @@' ; title2 'and use of do loop'; run; /* */ /* */ /* read Scott Singer's Gold & Silver */ /* brute strength and ignorance */ /* */ data gold ; infile 'C:\Users\sumi\Desktop\NCSU\SU12\Data_Sets\srsinger.dat' ; /* first 3 are London */ sum = 0. ; input gs date mmddyy10. price ; * first time ; sum = sum + price ; input gs date mmddyy10. price ; * second ; sum = sum + price ; input gs date mmddyy10. price ; * third ; sum = sum + price ; price = sum / 3. ; city = 'London' ; output ; /* next are Paris and Zurich */ input gs date mmddyy10. price ; city = 'Paris' ; output ; input gs date mmddyy10. price ; city = 'Zurich' ; output ; /* next six are New York */ sum = 0. ; input gs date mmddyy10. price ; sum = sum + price ; input gs date mmddyy10. price ; sum = sum + price ; input gs date mmddyy10. price ; sum = sum + price ; input gs date mmddyy10. price ; sum = sum + price ; input gs date mmddyy10. price ; sum = sum + price ; input gs date mmddyy10. price ; sum = sum + price ; price = sum / 6. ; city = 'New York' ; output ; /* next seven are silver prices */ input gs date mmddyy10. price ; /* read 'em and do nothing */ input gs date mmddyy10. price ; input gs date mmddyy10. price ; input gs date mmddyy10. price ; input gs date mmddyy10. price ; input gs date mmddyy10. price ; input gs date mmddyy10. price ; format date date.; run ; /* */ /* print out what we've got */ proc print data=gold ; title "S Singer's gold data" ; run ; /* */ /* fancy graphics, y*x=z plot */ proc gplot data=gold ; plot price*date=city ; title 'Gold prices by date and location' ; format date date.; run ;