BookmarkSubscribeRSS Feed
Ankur32
Obsidian | Level 7

Hello,

 

I wan to write a program to count missing and non missing values across the columns. I got a program from someone like this-

 

data temp;
input x y z a b$;
cards;
1 23 24 50 AA
1 . 24 50 AC
1 13 . 50 AB
1 23 . 50 .
;
run;

 

proc contents data=temp out=cols noprint;
run;

 

data _null_;
set cols nobs=total;
call symputx('totvar', total);
run;

 

data outdata;
set temp;
totalvar=&totvar;
totmiss=cmiss(of x--b);
totnonmiss=totalvar- cmiss(of x--b);
proc print ;
run;

 

and I got the correct result.

 

 

xyzabtotalvartotmisstotnmiss
1232450AB514
1.2450AC505
113.50AB514
123.50 523

 

 

 

 

But in the above program, there are 4 observations, and NOBS is used to calculate the number of observations, so how can we calculate number of variables. I mean when we use "set cols nobs=total;" the value of total should be 4 and the value of &totvar should also be 4, but in output it is showing 5. How? I am confused. I am not able to understand this. Please help me.

3 REPLIES 3
Kurt_Bremser
Super User

The answer is here:

proc contents data=temp out=cols noprint;
run;

data _null_;
set cols nobs=total;
call symputx('totvar', total);
run;

The output dataset of proc contents (cols) contains one observation for each variable in the input dataset (temp).

So when that is read in the next data step, the nobs comes from cols (not from temp!) and therefore is equivalent to the count of variables in temp.

Ankur32
Obsidian | Level 7

Thank you so much..!!

Ksharp
Super User
data temp;
input x y z a b$;
cards;
1 23 24 50 AA
1 . 24 50 AC
1 13 . 50 AB
1 23 . 50 .
;
run;

%let dsid=%sysfunc(open(temp));
%let totvar=%sysfunc(attrn(&dsid,nvar));
%let dsid=%sysfunc(close(&dsid));

%put &totvar;

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 9391 views
  • 0 likes
  • 3 in conversation