Desktop productivity for business analysts and programmers

Program to find a variable

Reply
Contributor
Posts: 44

Program to find a variable

Dear Users,

 

I'm new to SAS so I'm not entirely sure if something like this is even possible so would apologies if it's not. 

 

At present I have a dataset which has information on over 1,000,000 individuals. Moreover, the dataset also consists of over 400 variables. 

 

What I would like to do is aggregate each variable to get a total and then compare each total to a particular number to understand if there's a match.

 

So I'd like to tell SAS to sum variable x1 then x2, x3, all the way to x400. 

 

From there I'd like to be able to see does the aggregate of x1 = 500, or does x2, x3 etc. Simply put does any of these 400 variables match 500 and if so which one.

Hopefully, I've been clear on what I'd like to do and any comments would be great.

 

Kind regards,

Sean 

Super User
Posts: 19,098

Re: Program to find a variable

Your best bet for more help is to post sample data and expected output.
Because you have 400 variables you may also want to thoroughly familiarize yourself with variables lists, ways to reference variables withou explcilty typing each name.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

 

Are you coding or using GUI (tasks)  in EG? 

 

For summarizing data use PROC MEANS/SUMMARY

 

 

For filtering ata across a wide range of variables look at WHICHN function with a data step and/or an Array. 

 

Your best bet for more help is to post sample data and expected output. 

Because you have 400 variables you may also want to thoroughly familiarize yourself with variables lists, ways to reference variables withou explcilty typing each name. 

 

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

Super User
Posts: 7,422

Re: Program to find a variable

You can define an array over those variables:

array all_my_xs {*} x1=x400;

Then you can loop through the array.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,362

Re: Program to find a variable

If you're new to SAS, I suspect you need a little more guidance on this.  This would be a good starting point:

 

proc summary data=have;

var _numeric_;

output out=stats (drop=_type_ _freq_) sum=;

run;

 

This gives you a data set named STATS with a single observation.  All the original variable names are there, but they now hold the sum across all the original observations.

 

To search for all variables with a sum of 500, you could proceed:

 

data vars_500;

set stats;

length varname $ 32;

array nums {*} _numeric_;

do _n_=1 to dim(nums);

   if nums{_n_}=500 then do;

      varname = vname(nums{_n_});

      output;

   end;

end;

run;

 

In VARS_500, you will have a list of all variable names that summed to 500.

 

Ask a Question
Discussion stats
  • 3 replies
  • 208 views
  • 0 likes
  • 4 in conversation