How select all variables which have same end,
ex. aeSAF1 beSAF1 ceSAF1 i need to select all variables ending with SAF1
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.
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.
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
@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.
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;
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 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.
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
Someone posted this yesterday, it may be helpful to you:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.