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

Good morning,

 

I would like to know how to capture the number of observations and variables in a sas dataset and also summing a numeric vari called tran_amount all in one report. I know proc contents will give me the number obs and var. I need something like numobs=, numbervar= and suum_tran_amount = all in one report.

 

Thank you!

 

-mauri

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Riteshdell
Quartz | Level 8

Try this, Might be you are looking for this.

Note : you can sum logic same like this , and can use want table.

 

data have;
x='10'; output;
x='11';output;
y=11;output;
run;

%macro calculation;
proc sql;
select count(*) into:cnt_obs from have;
quit;
%put &cnt_obs;

proc contents data= have out=structure(keep=name type);
run;

proc sql;
select count(*) into:cnt_var from structure;
quit;
%put &cnt_var;

data want;
numobs=&cnt_obs;
numbervar=&cnt_var;
run;

%mend calculation;

%calculation;

View solution in original post

6 REPLIES 6
Karen_Horton
Obsidian | Level 7

Have you tried using PROC SQL? This will do what you need quickly and easily.

 

Suzanne_McCoy
Fluorite | Level 6

I always use the dictionary tables versus using proc contents.  I use this a lot for creating data dictionaries for the programs.

Something like this:

Proc sql;

 select *

 from dictionary.columns

 where libname= 'WORK' /*is always uppercase*/

    and memname='DATASETNAME' /* is always upcase */

...

 ;

quit;

 

Note that select * from dictionary.columns tell you about the columns in the table like length, type, format, informat, etc. while select * from dictionary.tables gives the number of logical obs (nlobs) and the number of columns/variables in the table/dataset/database table.

 

Riteshdell
Quartz | Level 8

Try this, Might be you are looking for this.

Note : you can sum logic same like this , and can use want table.

 

data have;
x='10'; output;
x='11';output;
y=11;output;
run;

%macro calculation;
proc sql;
select count(*) into:cnt_obs from have;
quit;
%put &cnt_obs;

proc contents data= have out=structure(keep=name type);
run;

proc sql;
select count(*) into:cnt_var from structure;
quit;
%put &cnt_var;

data want;
numobs=&cnt_obs;
numbervar=&cnt_var;
run;

%mend calculation;

%calculation;
Tom
Super User Tom
Super User

Since the first two are summaries of metadata about the data set and the last is a summary about the data in the dataset your will need to use two separate steps (or subqueries) to get that information.

 

Let's say you have the dataset HAVE.  Since you already know how to get the first two let's assume you have already created that in a dataset named CONTENT_SUMMARY.  Now you just need to get the SUM of your variable and combine them.

proc summary data=have ;
  var tran_amount;
  output out=data_summary sum=sum_tran_amount;
run;

And then combine the two.

data want;
  set content_summary;
  set data_summary;
run;
tsap
Pyrite | Level 9

I wasn't sure exactly how you wanted the specific items you requested to be accessed so I put the values into Macro Variables that can be referenced however you want. I build the logic in a macro statement but it could just have easily been a simple data step. I went with the macro statement instead so that the data step could easily be used across multiple datasets throughout a process if needed (so long as the values from one run are used prior to the values of a separate tables run otherwise the macro values will overwrite one another).

 

%MACRO TableDtls(TableName, InputSumVar, OutputCumSumVar);
	DATA _NULL_;
		DSID = OPEN("&TableName.");
			Vars = ATTRN(DSID,"NVARS");
			CALL SYMPUTX('Total_Variables',Vars, 'G');            /* put variable count in macro variable */
			CALL SYMPUTX('Total_Observations',&SQLOOPS., 'G');            /* put variable count in macro variable */
		RC = CLOSE(DSID); 
		SET &TableName.;
		FORMAT &OutputCumSumVar. 5.1;
		RETAIN &OutputCumSumVar.;
		IF _N_=1 THEN &OutputCumSumVar. = &InputSumVar.;
		ELSE &OutputCumSumVar. = (&OutputCumSumVar. + &InputSumVar.);
		CALL SYMPUTX("&OutputCumSumVar",&OutputCumSumVar., 'G');            /* put variable count in macro variable */
	RUN;

	%PUT &=Total_Variables;
	%PUT &=Total_Observations;
	%PUT &OutputCumSumVar.= &&&OutputCumSumVar.;
%MEND TableDtls;

%TableDtls(SASHELP.class, Weight, Sum_Weight);

So when you call the TableDtls Macro, you will provide 3 pieces of information:

     - First, the name of the table that you want to get the observation count, variable count, and summary details

     - Second, the name of the variable that you want the summary details

     - Third, the name that you want for the new macro variable that will resolve to the sum of the previously supplied variable

 

The three new Macro variables generated will resolve with these values:

TOTAL_VARIABLES=5
TOTAL_OBSERVATIONS=19
Sum_Weight= 1900.5

Confirming that the SASHELP.Class table contains:

    - 5 variables

    - 19 total observations

    - And the sum of the 19 weight values equals 1900.5.

 

Here is the data in the SASHELP.Class table for review:

Obs     Name     Sex     Age     Height     Weight 
1       Alfred   M        14       69.0      112.5 
2       Alice    F        13       56.5       84.0 
3       Barbara  F        13       65.3       98.0 
4       Carol    F        14       62.8      102.5 
5       Henry    M        14       63.5      102.5 
6       James    M        12       57.3       83.0 
7       Jane     F        12       59.8       84.5 
8       Janet    F        15       62.5      112.5 
9       Jeffrey  M        13       62.5       84.0 
10      John     M        12       59.0       99.5 
11      Joyce    F        11       51.3       50.5 
12      Judy     F        14       64.3       90.0 
13      Louise   F        12       56.3       77.0 
14      Mary     F        15       66.5      112.0 
15      Philip   M        16       72.0      150.0 
16      Robert   M        12       64.8      128.0 
17      Ronald   M        15       67.0      133.0 
18      Thomas   M        11       57.5       85.0 
19      William  M        15       66.5      112.0

 

Hope this helps.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1687 views
  • 2 likes
  • 7 in conversation