BookmarkSubscribeRSS Feed
sahoositaram555
Pyrite | Level 9

Hi everyone, i have a data set similar to below with 60 columns(both numeric and character). I'm planning to take then in to loop one by one and perform descriptive analysis. before they enter into the loop they must undergo a checking for their datatype. This is an interesting task which i'm planning to perform by the help of using arrays(as I've heard of the power of using arrays and used before).

 

After going through several white papers i have came up with a sample untested code and it would be great to have your help to take my array learning to a next level.  

 

Kindly do comment.

 

data temp;
input subjid AGE SEX BMI SODIUM INFECTION;
cards;
01001 56 F 22.3 34.5 Y

01001 60 M 29.3 41.5 N

01001 35 F 26.3 22.5 Y;

01001 23 M 19.3 34.5 Y;

01001 34 F 18.3 41.5 Y;

;
run;

 

%macro descriptive(first=, last= );

data _null_;

set temp;

array variables AGE--INFECTION;

%do over variables;

%if datatyp(variables[i] eq CHARACTER) %then %do;

proc freq data=temp;

var variables[i];

run;

%end;

%else %if datatyp(variables[i] eq NUMERIC) %then %do;

proc means data =temp;

var variables[i];

run;

%end;

%mend descriptive;

 

2 REPLIES 2
Reeza
Super User
Arrays can only hold one type of variable so this won't work for you. Instead for what you're trying to do, I suggest looking at the VINFO() and maybe VNEXT() or instead consider getting the types from proc contents ahead of time or sashelp.vcolumn (dictionary.column) table.

You could also use _character_ to refer to all character variables or _numeric_ to refer to all numeric.
Or AGE-numeric-INFECTION to reference only numeric variables between the variables.

Note that you cannot nest your code in that manner either, you cannot nest PROC FREQ within a data step or PROC MEANS. So your entire code structure is problematic.

If you want to summarize all variables from a data set instead use the following code:

proc freq data=temp;
table AGE-character-Infection;
run;

proc means data=temp;
var AGE-numeric-Infection;
run;

That will run all at once. I'll leave you to determine how you want to turn that into a macro.

Note you have first/last in your macro definition but never use them.
Reeza
Super User

Some references for you:

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

UCLA introductory tutorial on macro variables and macros

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 665 views
  • 0 likes
  • 2 in conversation