DATA Step, Macro, Functions and more

Select many variabled

Reply
Contributor
Posts: 38

Select many variabled

How select all variables which have same end, 

ex. aeSAF1 beSAF1 ceSAF1 i need to select all variables  ending with SAF1

Super User
Super User
Posts: 9,399

Re: Select many variabled

In what sense?  I.e. do you mean in an sql select, a keep statement, in an array?  In datastep you can use the : to indicate all with prefix of and numeric suffix, e.g.:

test=sum(of aesaf:);

Assuming all the variables are numeric, have the prefix of aesaf, and then have a number after, like: aesaf1, aesaf2 etc.

You can also use ranges:

test=sum(of aesaf1--aesaf10);

To indicate all variables which logically appear between aesaf1 and aesaf10.

 

Context is everything in questions, follow the guidance, post test data in the form of a datastep, show what you want out at the end etc.  Its hard to suggest anything with no information to go on.

Contributor
Posts: 38

Re: Select many variabled

my example bellow
aSAF1 bSAF1 cSAF1 eSAF1 i need to select when SAF1 in the end
Super User
Super User
Posts: 9,399

Re: Select many variabled

[ Edited ]

This does not tell me anything.  Please review the guidance for posting a new question - post test data in the form of a datastep, what the output should look like.  What you have written, what logs etc.  The term select is vague.  Let me explain:

 

Example question:

I have a dataset which looks like:

data have;
  infile datalines;
  input aesaf1 aesaf2 aesaf3;
datalines;
1 2 3
;
run;

I need to sum these variables into a new variable called result.  My output should look like:

aesaf1  aesaf2  aesaf3  result

1           2           3          6

 

Response:

Here is code which does what you ask;

data want;
  set have;
  result=sum(of aesaf:);
run;

 

For example, if you want to keep only those variables mentioned then:

data want (keep=asaf1--esaf1);
  set have;
run;

You could transpose the dataset (assuming that the variables are the same) - this would be ideal as working with normalised (goes down rather than across) is far far easier and avoids problems like this that your facing.

Contributor
Posts: 38

Re: Select many variabled

you give me simple one with numeric end,that was very simple we can use like a1-a10 or... I need what I write in comment with same end part . Thanks

Valued Guide
Posts: 515

Re: Select many variabled


@Vahe_Mar wrote:
my example bellow
aSAF1 bSAF1 cSAF1 eSAF1 i need to select when SAF1 in the end

Unfortunately there is no easy way to do this.

Super Contributor
Posts: 331

Re: Select many variabled

Hello,

 

data _NULL_;
    set have;

    call execute('data want; set have; keep');

    do while (varname ne "varname");
        call vnext(varname);
        suffix=substr(varname,length(varname)-3);
        if suffix="SAF1" then do;
            call execute(varname);
        end;
    end;

    call execute('; run;');
    stop;
run;
Super User
Posts: 9,870

Re: Select many variabled

Having the dynamic part of the variable names in front instead of at the end is of course suboptimal, as it precludes the use of the : wildcard that SAS provides.

You will be forced to read variable names from dictionary.columns with a contains condition or similar, and put them into a macro variable with the into: clause:

proc sql noprint;
select name into :varlist separated by " "
from dictionary.columns
where libname = 'LIBRARY' and memname = 'DATASET' and name contains 'SAF1';
quit;

proc print data=library.dataset;
var &varlist;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 38

Re: Select many variabled

Posted in reply to KurtBremser
Thanks, that is right decision , but data in work and in sas help only present datas from data
Super User
Posts: 9,870

Re: Select many variabled


@Vahe_Mar wrote:
Thanks, that is right decision , but data in work and in sas help only present datas from data

???

 

I have no clue what you mean.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 38

Re: Select many variabled

Posted in reply to KurtBremser
I tried to use that sql , with data from sashelp which contains variable name information, but in that library only contains data from "data" library not from work
Super User
Posts: 9,870

Re: Select many variabled

Post your code, please.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,237

Re: Select many variabled

SAS doesn't work well with suffixes unfortunately.

 

If you have any control over the naming structure use a prefix method instead and then you can shortcut list them.

 

PROC SQL does not support variables lists at all though.

 


@Vahe_Mar wrote:

How select all variables which have same end, 

ex. aeSAF1 beSAF1 ceSAF1 i need to select all variables  ending with SAF1


 

Super User
Posts: 23,237

Re: Select many variabled

Ask a Question
Discussion stats
  • 13 replies
  • 131 views
  • 0 likes
  • 6 in conversation