BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mdhtrivedi
Obsidian | Level 7

Hello,

      I have a dataset named Subset.csv". I want to count the number of 'NA' occuring in the dataset and want to print that. My dataset looks like below:-

 

Day5Day6Day7City1City2City3
FridayNANANADelhi/NCRNA
NANANANANAMumbai
FridayNANANANANA
FridaySaturdayNABangaloreNANA
NANANANADelhi/NCRNA
FridaySaturdaySundayBangaloreNANA
NANANANADelhi/NCRNA
NANANANADelhi/NCRNA
NANANANANANA
FridaySaturdayNANANANA

 

As an output I want in output window:-

 

count=44

 

I have tried below code snippet to achieve the result:-

data Subset;
		array SubsetArray{10,6} Day5-Day7 City1-City3; 
		do i=1 to 10;
     		do j=1 to 6;
     			if SubsetArray(i,j) = 'NA' then
       		    count+1;
     		end;
		end;
 proc print count;    
run;

But, I am getting error of "Too few variables defined for the dimension(s) specified for the array SubsetArray."

I am not sure whether I have defined two dimensional array in a wrong way.

Please help me to achieve my task.

Thank You

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Proper syntax:

 

data Subset;
set have end=done;
array a Day5-Day7 City1-City3; 
do i=1 to dim(a);
    count + (a{i} = "NA");
    end;
if done then output;
keep count;
run;

proc print data=subset;    
run;

untested

 

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

Proper syntax:

 

data Subset;
set have end=done;
array a Day5-Day7 City1-City3; 
do i=1 to dim(a);
    count + (a{i} = "NA");
    end;
if done then output;
keep count;
run;

proc print data=subset;    
run;

untested

 

PG
novinosrin
Tourmaline | Level 20

No need for arrays in my opinion

 

data have;
input (Day5	Day6	Day7	City1	City2	City3) (:$20.);
cards;
Friday	NA	NA	NA	Delhi/NCR	NA
NA	NA	NA	NA	NA	Mumbai
Friday	NA	NA	NA	NA	NA
Friday	Saturday	NA	Bangalore	NA	NA
NA	NA	NA	NA	Delhi/NCR	NA
Friday	Saturday	Sunday	Bangalore	NA	NA
NA	NA	NA	NA	Delhi/NCR	NA
NA	NA	NA	NA	Delhi/NCR	NA
NA	NA	NA	NA	NA	NA
Friday	Saturday	NA	NA	NA	NA
;

data w;
set have end=lr;
k=count(cats(of _all_),'NA');
want+k;
if lr;
put want=;
keep want;
run;
ChrisNZ
Tourmaline | Level 20

If the words might contain the letters NA, something like this works:

data _null_;
  set HAVE end=LASTOBS; 
  COUNT + count(catt('~',catx('~~',of _all_),'~'),'~NA~');
  if LASTOBS;
  putlog COUNT=;
run;

 

mdhtrivedi
Obsidian | Level 7

with minor modifications, this worked. Thanks for the solution!!

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1549 views
  • 1 like
  • 4 in conversation