I have been trying to figure it out how to solve this question for two days but no luck yet. Hope you can help.
Q) Convert all consonants in the values for the City and Team variables to uppercase letters and all vowels in the values for the City and Team variables to lowercase letters.
Note: Overwrite the original values with the new values Refer the detail to the attachment.
infile ' /home/coccus030/sasuser.v94/nfldata2.txt' firstobs=2;
*/ @20 to indicate SAS to read at 20 city and year are too close to each other;
input City & $16. @20 Year Team $;
Spacec = city;
if index (strip(city),'') >0 then spacec =1;
else spacec = 0;
Spacet = Team;
if index (strip(team),'')>0 then spacet = 1;
else Spacet = 0;
Numbert = team;
*anydigit look for number;
if anydigit (team) then numbert =1;
else numbert = 0;
CityTeam = catx('-',city,team);
array CTL (3) $ city Team Cityteam;
do lowcase = 1 to substr('City',2,1);
If substr(lowcase(City),2,1) in ('a','e','i','o','u') then CTL (lowcase) = lowcase;
else CTL (lowcase) = upcase;
end;
superbowl = city;
if team = 'Giants' then superbowl = 'Winner';
else superbowl = 'not a contender';
run;
Txt
----+----1----+----2----+----3 Baltimore 2012 Ravens Cincinnati 2012 Bengals Cleveland 2012 Browns Pittsburgh 2012 Steelers Chicago 2012 Bears Detroit 2012 Lions Green Bay 2012 Packers
I would do:
data sports;
length City $15 Team $12;
infile datalines dlm=',' dsd;
input @1 City $ year Team $;
from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
to = "aBCDeFGHiJKLMNoPQRSTuVWXyZ";
city = translate(upcase(city),to,from);
team = translate(upcase(team),to,from);
drop from to;
datalines;
Baltimore, 2012, Ravens
Cincinnati, 2012, Bengals
Cleveland, 2012, Browns
Pittsburgh, 2012, Steelers
Chicago, 2012, Bears
Detroit, 2012, Lions
Green Bay, 2012, Packers
;
proc print; run;
Hi:
I agree with a DO loop, I'm not sure you need an ARRAY, since the TRANSLATE function will work for you. I did not consider 'y' to be a vowel for this example. Also, I did NOT overwrite the original values because I wanted to show my work in progress (TmpStr, NewCity and NewTeam):
Cynthia
Here's the code I used:
data sports;
length City NewCity $15 Team NewTeam $12 TmpStr $30 uchar $1;
infile datalines dlm=',' dsd;
input @1 City $ year Team $;
TmpStr=upcase(catx('~',City,Team));
do ck = 'a', 'e', 'i', 'o', 'u';
uchar = upcase(ck);
TmpStr = translate(TmpStr,ck,uchar);
end;
NewCity = scan(TmpStr,1,'~');
NewTeam = scan(TmpStr,2,'~');
return;
datalines;
Baltimore, 2012, Ravens
Cincinnati, 2012, Bengals
Cleveland, 2012, Browns
Pittsburgh, 2012, Steelers
Chicago, 2012, Bears
Detroit, 2012, Lions
Green Bay, 2012, Packers
;
run;
proc print data=sports;
var City NewCity year Team NewTeam TmpStr;
run;
I would do:
data sports;
length City $15 Team $12;
infile datalines dlm=',' dsd;
input @1 City $ year Team $;
from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
to = "aBCDeFGHiJKLMNoPQRSTuVWXyZ";
city = translate(upcase(city),to,from);
team = translate(upcase(team),to,from);
drop from to;
datalines;
Baltimore, 2012, Ravens
Cincinnati, 2012, Bengals
Cleveland, 2012, Browns
Pittsburgh, 2012, Steelers
Chicago, 2012, Bears
Detroit, 2012, Lions
Green Bay, 2012, Packers
;
proc print; run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.