* Using the LAG function; data citimon; set sashelp.citimon; ip1prev=lag(ip); ip2prev=lag2(ip); ip3prev=lag3(ip); ip4prev=lag4(ip);ip5prev=lag5(ip);ip6prev=lag6(ip); ip_movavg=mean(ip,ip1prev,ip2prev,ip3prev); ip_movavg_long=mean(ip,ip1prev,ip2prev,ip3prev,ip4prev,ip5prev,ip6prev); run; proc print; var date ip ip1prev ip2prev ip3prev ip_movavg; run; proc sgplot; series x=date y=ip; series x=date y=ip_movavg; run; proc sgplot; series x=date y=ip; series x=date y=ip_movavg_long; run; * Using COUNT, COUNTW, and COUNTC; data zips; set sashelp.zipcode; *Counting alternate names of cities; number_alt_names=0; if Alias_City ne missing THEN number_alt_names=number_alt_names+1; if Alias_CityN ne missing THEN number_alt_names=number_alt_names+1; number_alt_names_more=count(Alias_CityN,'||') + count(Alias_City,'||'); number_alt_names_final=number_alt_names+number_alt_names_more; drop number_alt_names number_alt_names_more; run; proc print data=zips (obs=200); var city statecode Alias_City Alias_CityN number_alt_names_final; run; *Just looking at SC cities/zip codes is interesting; proc print data=zips; var zip city statecode Alias_City Alias_CityN number_alt_names_final; WHERE statecode='SC'; run; *Longest names of cities by number of words; *Also: Longest (and shortest) names of cities by number of characters; proc sort data=sashelp.zipcode nodupkey out=mycities; /* Keeping only unique city-state observations */ by statecode city; run; data mycities; set mycities; num_words=countw(City); LABEL num_words = 'Length of the City Name (in words)'; num_chars=lengthn(City); LABEL num_chars = 'Length of the City Name (in characters)'; num_qs=countc(City,'q', 'i'); *the i modifier tells sas to IGNORE case; LABEL num_qs = 'Number of Qs in the City Name'; num_vowels=countc(City,'aeiou', 'i'); *the i modifier tells sas to IGNORE case; LABEL num_vowels = 'Number of vowels in the City Name'; run; proc sort data=mycities out=cities_w; BY DESCENDING num_words; *by number of words, from longest to shortest; run; proc print data=cities_w (OBS=70) label; VAR City statecode num_words; run; proc sort data=mycities out=cities_cd; BY DESCENDING num_chars; *by number of characters, from longest to shortest; run; proc print data=cities_cd (OBS=30) label; VAR City statecode num_chars; run; proc sort data=mycities out=cities_c; BY num_chars; *by number of characters, from shortest to longest; run; proc print data=cities_c (OBS=30) label; VAR City statecode num_chars; run; proc sort data=mycities out=cities_q; BY DESCENDING num_qs; *by number of Qs, from longest to shortest; run; proc print data=cities_q (OBS=20) label; VAR City statecode num_qs; proc sort data=mycities out=cities_v; BY DESCENDING num_vowels; *by number of vowels, from longest to shortest; run; proc print data=cities_v (OBS=20) label; VAR City statecode num_vowels; run; data staff; input name $ 1-30 department :$15.; lines; Johanna Dylan, B.A. English Paul Krem, Ph.D. Science John Prime, M.S. Mathematics Ronnie Fisher, B.S. Mathematics Craig Tolleson, Ed.D. Science Consuelo Gomez, M.A. Languages Raymond Davies, M.A. English Jerry Cruncher, B.A. English Terence Stamp, Ph.D. Science Doris Champion, M.S. Science Connie Williams, B.S. Science Gerald Early, M.S. Mathematics Wright Rong, B.A. Languages Thomas Promise, B.S. Mathematics ; run; data staff; set staff; degree=strip(substr(name,find(name,',')+1)); * the STRIP function removes any leading or trailing blanks; if degree='Ph.D.' or degree='Ed.D.' or degree='J.D.' or degree='M.D.' then degree_type='Doctorate'; else if degree='M.S.' or degree='M.A.' or degree='M.F.A.' or degree='M.B.A.' then degree_type="Master"; else if degree='B.S.' or degree='B.A.' then degree_type="Bachelor"; else degree_type='N/A'; run; proc print; run; proc freq data=staff; tables degree_type / nocum; run; *Doing exactly the same thing another way, with SCAN; data staff3; set staff; degree=scan(name,-1,' '); if degree='Ph.D.' or degree='Ed.D.' or degree='J.D.' or degree='M.D.' then degree_type='Doctorate'; else if degree='M.S.' or degree='M.A.' or degree='M.F.A.' or degree='M.B.A.' then degree_type="Master"; else if degree='B.S.' or degree='B.A.' then degree_type="Bachelor"; else degree_type='N/A'; run; proc print; run; proc freq data=staff3; tables degree_type / nocum; run; *Using findw; proc sort data=sashelp.zipcode nodupkey out=mycities; /* Keeping only unique city-state observations */ by statecode city; run; data mycitiesSC; set mycities; BasePos=findw(City,'Base'); BaseWord=findw(City,'Base',' ','e'); IF statecode='SC'; *if BasePos gt 0; *only include the cities containing the word Base; run; proc print; var city statecode BasePos BaseWord; run;