DATA Step, Macro, Functions and more

How to refer a long list of variables instead of typing individually?

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

How to refer a long list of variables instead of typing individually?

[ Edited ]

Dear all,

 

I have many categories of variables and each of category include a lot of variables. I would like to create a statement that for each individual observation,  if there is at least one variable has non missing value, then we can use this observation. Now I only know the statement like below:

if apple ne . or banana ne . or  grape ne. then flag='yes'; 

However, since I have tons of variables in each category, it will be very time consuming to type them individually. I'm wondering if there is an efficient way to do it.

 

I appreciate any thoughts and recommendations.

Thank you!


Accepted Solutions
Solution
‎02-26-2018 01:34 PM
Super User
Posts: 23,295

Re: How to refer a long list of variables instead of typing individually?

Use the N function to count the number of non missing values.

 

If n(of var1-var3) >=1 then do;

View solution in original post


All Replies
Solution
‎02-26-2018 01:34 PM
Super User
Posts: 23,295

Re: How to refer a long list of variables instead of typing individually?

Use the N function to count the number of non missing values.

 

If n(of var1-var3) >=1 then do;
Contributor
Posts: 20

Re: How to refer a long list of variables instead of typing individually?

Thank you for your recommendation! but they are not consecutive variables. They locate at random place in a large dataset.

Super User
Posts: 6,632

Re: How to refer a long list of variables instead of typing individually?

If the variables you are concerned about are all numeric, you could use:

 

if n(of _numeric_) > 0 then do;

Super User
Posts: 23,295

Re: How to refer a long list of variables instead of typing individually?

If you don't have a naming convention or they're not placed sequentially then unfortunately you're stuck listing them all out. 


wwendy wrote:

Thank you for your recommendation! but they are not consecutive variables. They locate at random place in a large dataset.




Super User
Super User
Posts: 7,934

Re: How to refer a long list of variables instead of typing individually?

It would help if you named your variables from the beginning to account for this need.

if n(of fruit_:) > 0 then flag='yes';
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 120 views
  • 1 like
  • 4 in conversation