Hi there,
Hope guys, you are doing well these days.
I have one simple question.
Yes, it's simple but could not manage to find an appropriate solution 🙂
I have below mentioned 36 variables, and want to drop them in my final data step.
But I think it'll be not accurate with just mention them in the drop statement.
Can anyone suggest another approach?
Thank you in advance
@webart999ARM wrote:
Hi there,
Hope guys, you are doing well these days.
I have one simple question.
Yes, it's simple but could not manage to find an appropriate solution 🙂I have below mentioned 36 variables, and want to drop them in my final data step.
But I think it'll be not accurate with just mention them in the drop statement.
Can anyone suggest another approach?
What do you mean "not accurate"? Why do you think the DROP statement will not work; the DROP statement does exactly what it is supposed to do.
I don't say that il will not work. I'm just seeking a shorter solution.
@webart999ARM wrote:
I don't say that il will not work. I'm just seeking a shorter solution.
Shorter? How about the solution from @PGStats above?
If that's not it, explain the criteria you would want to use to determine what variables to drop.
"drop _9t_9a -- _7t_fb;" it's an obviously shorter solution, but '--' can cause unexpected results after data update, I think you know what I'm talking about.
The solution for that could be to define variables in that exact order before the drop, but in that case, again we will have 36 variables mentioned in 'length' or 'retain' statement.
I will use 'keep' for now.
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
You can short cut your references if you have a naming convention. The article above illustrates the methods to shorten a list of variables and make them more dynamic. You can drop all variables that start with _9T with the following line for example.
drop _9T: ;
@webart999ARM wrote:
"drop _9t_9a -- _7t_fb;" it's an obviously shorter solution, but '--' can cause unexpected results after data update, I think you know what I'm talking about.
The solution for that could be to define variables in that exact order before the drop, but in that case, again we will have 36 variables mentioned in 'length' or 'retain' statement.
I will use 'keep' for now.
If they appear in that order, you can say
drop _9t_9a -- _7t_fb; /* note the double dashes */
or you could simply mention the variables that you want to keep in a keep statement. The others will be dropped.
There are several ways of mentioning multiple variables in a DROP statement (or elsewhere):
If none of these work for you, you may have to do some more advanced stuff. One is using a macro to define the exact names:
%macro droplist(prefixes,suffixes);
%local i1 i2 w1;
%do i1=1 %to %sysfunc(countw(&prefixes));
%let w1=%scan(&prefixes,&i1);
%do i2=1 %to %sysfunc(countw(&suffixes)); _&w1._%scan(&suffixes,&i2)%end;
%end;
%mend;
data want;
set have;
drop %droplist(9T 8T 3T 7T,AA AB BA BB DA EB FB) _dummy xx p3;
run;
I put in the extra variables in the DROP example, just to show how the macro can be used very flexibly.
Sounds like you want an easy text pattern generator.
data _null_;
length text $1024.;
do i = 9,8,7,3;
do j = 'AA','AB','BA','BB','CA','CB','DA','EB','FB';
text = catx(' ',text,cats('_',i,'T_',j));
end;
end;
call symputx('text',text);
run;
%put &=text;
Sure you can transform it to macro.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.