DATA Step, Macro, Functions and more

UpcaseLowcase

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

UpcaseLowcase

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

 


Accepted Solutions
Solution
‎04-20-2018 06:54 AM
Esteemed Advisor
Posts: 5,392

Re: UpcaseLowcase

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;
PG

View solution in original post


All Replies
SAS Super FREQ
Posts: 9,254

Re: UpcaseLowcase

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):

upcase_lowcase.png

 

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;
Solution
‎04-20-2018 06:54 AM
Esteemed Advisor
Posts: 5,392

Re: UpcaseLowcase

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;
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 109 views
  • 1 like
  • 3 in conversation