BookmarkSubscribeRSS Feed
webart999ARM
Quartz | Level 8

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.

webart999ARM_0-1596548732016.png

 

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

 

 

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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

webart999ARM_0-1596548732016.png

 

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.

--
Paige Miller
webart999ARM
Quartz | Level 8

I don't say that il will not work. I'm just seeking a shorter solution.

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
webart999ARM
Quartz | Level 8

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

Reeza
Super User

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.


 

PGStats
Opal | Level 21

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.

PG
s_lassen
Meteorite | Level 14

There are several ways of mentioning multiple variables in a DROP statement (or elsewhere):

  • drop _9T_AA--7T_FB - drops the variables if they are in that order on the data step data vector
  • drop _: - drops all variables beginning with an underscore. I very often define temporary variables with an underscore first, and then use exactly that statement to drop them
  • drop _9T_: _8T_: _3T_: _7T_: - a variation on the above, a bit more refined, should get all the variables mentioned

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.

 

 

whymath
Lapis Lazuli | Level 10

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.

webart999ARM
Quartz | Level 8
Thank you all guys for your suggested methods.
I find some new techniques for me.

Special thanks to @s_lassen for an advanced solution. I will try to understand the code you have sent 🙂

#bestcommunity

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1184 views
  • 1 like
  • 6 in conversation