BookmarkSubscribeRSS Feed
Vahe_Mar
Obsidian | Level 7

How select all variables which have same end, 

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

Thanks,
Vahe
13 REPLIES 13
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Vahe_Mar
Obsidian | Level 7
my example bellow
aSAF1 bSAF1 cSAF1 eSAF1 i need to select when SAF1 in the end
Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Vahe_Mar
Obsidian | Level 7

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

Thanks,
Vahe
andreas_lds
Jade | Level 19

@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.

gamotte
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User

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;
Vahe_Mar
Obsidian | Level 7
Thanks, that is right decision , but data in work and in sas help only present datas from data
Thanks,
Vahe
Vahe_Mar
Obsidian | Level 7
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
Thanks,
Vahe
Reeza
Super User

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


 

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
  • 13 replies
  • 1574 views
  • 0 likes
  • 6 in conversation