SAS Two dimensional Arrays

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

SAS Two dimensional Arrays

Hello

 

I have a data set which has character variables as days and cities.

 

Each variable has observation as either day name or missing vlaue 'NA' for variables with day Name. Similary,Each variable has observation as either city name or missing vlaue 'NA' for variables with City Name.

 

I would like to count the number of missing vlaues in those character variables with SAS two dimensional array.

 

I have attached the sample data please help me regarding this.


Accepted Solutions
Solution
‎03-24-2016 07:55 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

Thats beacuse your variables do not have a numerical suffix, e.g. VAR1, VAR2 etc.  You would need to specify the variables:

array var{2,7} monday tuesday wednesday...

 

As I mentioned in a previous post, it is Not a god idea to put "data" in variable names - the above is a good illustraion of why not.  Column names are ther for programming purposes - to make your programming simpler - not to describe data, that is the purpose of a label.

 

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

Hi,

 

Can you be a bit more specific please.  Post your test data in the form of a datastep so I don't have to type it in, provide and example of what your output should look like.  Are you summing across rows, if so do you want two counts for each block, or is it by column etc.  What relationship does the data have, i.e. does the Tuesday NA in first observation relate to Mumbai in Mumbai.  Why are column names "data" elements?  It would make your life easier if you just called each variable VAR1 VAR2 etc.  Saves you coding.  Also a strcuture change would also make your life far easier:

PARAM              RESULT

Monday              NA

Tuesday             NA

Bangalore           NA

Mumbai              Mumbai

...

Contributor
Posts: 68

Re: SAS Two dimensional Arrays

Please find the data set in the attachment. It will be helpful.

 

I would like to find missing values in character variables using two dimensional arrays.

 

As we have done in example 3 of below link.

 

http://www.ats.ucla.edu/stat/sas/library/multidimensional_arrays.htm

 

Kafeel

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

I am afraid I am not downloading or importing files, which is why I asked for test data in the form of a datastep.  As such, I can only provide a guess at the code, which you pretty much have in that example you give.  Remember an array its self is nothing more than a reference to a set of variables, i.e. its a shortcut to refering to variables.  For your example you give, there really isn't any point in declaring an array as you only have two variables of each kind, so you can do something like this:

data have;
  input monday $ tuesday $ bangalore $ mumbai $;
datalines;
NA     NA       NA        Mumbai
Monday Tuesday  NA        NA
NA     Tuesday  Bangalore NA
NA     NA       NA        Mumbai
Monday NA       NA        NA
;
run;

data want;
  set have;
  m1=ifn(monday="NA",0,1) + ifn(tuesday="NA",0,1);
  m2=ifn(bangalore="NA",0,1) + ifn(mumbai="NA",0,1);
run;

Simple.  If you have to do it for a learning exercise, then this works - however you will see its far more code and not ideal in this scenario:

data want;
  set have;
  array var{2,2} monday tuesday bangalore mumbai;
  array m{2} 8;
  do i=1 to 2;
    do j=1 to 2;
      m{i}=sum(m{i},ifn(var{i,j}="NA",0,1));
    end;
  end;
run;

 

Contributor
Posts: 68

Re: SAS Two dimensional Arrays

This helps.

 

As you have used input statement with datalines to input the variable values.

 

Can't we just import the data using proc import and use set statement?.

 

Kafeel

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

Yes, you can import your data however you like.  I don't download files, so I just copied and pasted from the original text file.  This is what I meant about posting data in the form of a datastep, it means that we only have to run the datastep and have the data ready, no importing, or manipulating of files and such like.

Contributor
Posts: 68

Re: SAS Two dimensional Arrays

I tried as following

 

proc import datafile="Path/Online Orders.csv"
out=BS
dbms=csv replace;
run;
data want;
set BS;
array var{2,7} Monday-Sunday Bangalore-Calcutta;
array m{2};
do i=1 to 7;
do j=1 to 7;
m{i}=sum(m{i},ifn(var{i,j}="NA",0));
end;
end;
run;

 

Getting error as below, need help

 

56 data want;
57 set BS;
58 array var{2,7} Monday-Sunday Bangalore-Calcutta;
ERROR: Missing numeric suffix on a numbered variable list (Monday-Sunday).
ERROR: Alphabetic prefixes for enumerated variables (Bangalore-Calcutta) are different.
ERROR: Too few variables defined for the dimension(s) specified for the array var.
59 array m{2};
60 do i=1 to 7;
61 do j=1 to 7;
62 m{i}=sum(m{i},ifn(var{i,j}="NA",0));
___
71
ERROR 71-185: The IFN function call does not have enough arguments.
Solution
‎03-24-2016 07:55 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

Thats beacuse your variables do not have a numerical suffix, e.g. VAR1, VAR2 etc.  You would need to specify the variables:

array var{2,7} monday tuesday wednesday...

 

As I mentioned in a previous post, it is Not a god idea to put "data" in variable names - the above is a good illustraion of why not.  Column names are ther for programming purposes - to make your programming simpler - not to describe data, that is the purpose of a label.

 

Contributor
Posts: 68

Re: SAS Two dimensional Arrays

Hello

 

I have one last question can you please tell me why you have used 0 and 1 in the below code as it was not there in that dataset.

 

ifn(monday="NA",0,1)

 

Once I removed 0 and 1 I am getting error on 'ifn'.

 

Kafeel

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS Two dimensional Arrays

ifn() is a function.  It takes three parameters:

1 - a logical test

2 - the number to be returned if the logical test is true

3 - the number to be returned if the logical test is false

 

m{i}=sum(m{i},ifn(var{i,j}="NA",0,1));

 

What is basically states is to sum the values m{i}, and the result of var{i,j}="NA", if its NA then add 0, otherwise add 1.

 

 

Respected Advisor
Posts: 3,775

Re: SAS Two dimensional Arrays

[ Edited ]

You could use a name range Sunday--Saturday  notice two dashes if the variables are in order in the data set.

 

Learning how to use the "SAS Variable List" will take you to another level of SAS programming to win friends and influence people. Robot wink

 

http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 966 views
  • 1 like
  • 3 in conversation