Help using Base SAS procedures

Arrays

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Arrays

I am trying to use an array to clean my data, I have tried a few different things and nothing seems to really change what I am seeing, either in the data or the log.

 

     array optionschar {4}  $ gender
                       Marital_Status
                       Game_Attendance
                       games_attended;
	        do i = 1 to 10;
                       if optionschar{i} = "Male" then optionschar{i} = "M";
                       if optionschar{i} = "m" then optionschar{i} = "M";
                       if optionschar{i} = "Female" then optionschar{i} = "F";
                       if optionschar{i} = "Mar" then optionschar{i} = "M";
                       if optionschar{i} = "Married" then optionschar{i} = "M";
                       if optionschar{i} = "Single" then optionschar{i} = "S";
                       if optionschar{i} = "999" then optionschar{i} = ".";
                       if optionschar{i} = "Four" then optionschar{i} = "4";
        end;
        drop i; 
keep gender Marital_Status Game_Attendance games_attended;
run;        
        

Accepted Solutions
Solution
‎02-18-2018 06:02 PM
PROC Star
Posts: 1,817

Re: Arrays

why are you looping 10 times when there are only 4 elements in your array. Shouldnt it be 

do i=1 to 4;

View solution in original post


All Replies
Solution
‎02-18-2018 06:02 PM
PROC Star
Posts: 1,817

Re: Arrays

why are you looping 10 times when there are only 4 elements in your array. Shouldnt it be 

do i=1 to 4;

Respected Advisor
Posts: 3,040

Re: Arrays

This doesn't even seem like a situation where arrays are useful.

 

You are performing different tasks on each variable in the array. Arrays are useful when you are performing the same task on each variable in the array.

--
Paige Miller
PROC Star
Posts: 1,817

Re: Arrays

use proc format and put function

New Contributor
Posts: 2

Re: Arrays

Posted in reply to novinosrin

Thank you both for the insight.

Super User
Posts: 23,754

Re: Arrays

Check multiple conditions at once using IN instead of the multiple IF statements:

 

if optionschar{i} = "Male" then optionschar{i} = "M";
if optionschar{i} = "m" then optionschar{i} = "M"

is the same as:

 

  if optionschar{i} in ("Male", 'm') then optionschar{i} = "M";

And another suggestion, make everything upper case in your comparison to avoid having to check for m, M, Male and male.  

 

if upcase(gender) in ('M', 'MALE') then gender='M';
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 467 views
  • 6 likes
  • 4 in conversation