BookmarkSubscribeRSS Feed
Jmitchell
Calcite | Level 5

Hi, I am working on a project and I have to drop any variable where the variable name begins with ‘Street’ in this merge data set.

 

This is my syntax:

libname bhadlibs 'C:\Users\PhotonUser\My Files\Temporary Files';
data bhadlibs.employee_payroll;
set bhadlibs.employeepayroll;
proc sort;by employee_id;

/* Experienced difficulty on renaming multiple naming conventions and dropping "street. We should review this in class.
data bhadlibs.employee_donations;
set bhadlibs.employee_donations;
proc sort;by employee_id;


data bhadlibs.employee_addresses (rename=(street_id=street));
set bhadlibs.employee_addresses;
proc sort;by employee_id;

data bhadlibs.employee_addresses (rename=(street_number=street));
set bhadlibs.employee_addresses;
proc sort;by employee_id;
run;

data bhadlibs.employee_addresses (rename=(street_name=street2));
set bhadlibs.employee_addresses;
proc sort;by employee_id;
run;*/

data bhadlibs.employee_addresses;
set bhadlibs.employee_addresses2 (drop=street);
footnote1 'confidential';
run;

 

data mergepart;
merge bhadlibs.employee_payroll bhadlibs.employee_donations bhadlibs.employee_addresses;
by employee_id;
proc print;
run;

4 REPLIES 4
Quentin
Super User

You can use a variable name prefix to drop variables, e.g.:

 

data mergepart (drop=Street: ) ;

That will drop any variables that start with Street (case insensitive).

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Jmitchell
Calcite | Level 5
Hi,

I'm still getting an error message. But I think there are a couple of other
issues. I keep getting an error saying my file doesn't exist.

How do I correct this?


PaigeMiller
Diamond | Level 26

Interesting, you say you want to drop any variable whose name begins with 'street', but then you don't do anything like that, there are no DROP statements or options anywhere in your code until the very end.

 

Furthermore, this is highly redundant and wastes your time and computer time.

 

data bhadlibs.employee_payroll;
set bhadlibs.employeepayroll;


First, good practice in writing code means you should end each step with a RUN command.

 

 

data bhadlibs.employee_payroll;
set bhadlibs.employeepayroll;
run;
proc sort;by employee_id;
run;

 

Now also notice that your DATA step does not do anything. You had a dataset named BHADLIBS.EMPLOYEEPAYROLL and you create a brand new dataset where nothing changes except the name, which is now BHADLIBS.EMPLOYEE_PAYROLL. Data steps with nothing but a SET statement are generally not worth doing.

 

How about this to drop variables?

 

data bhadlibs.employee_payroll;
    set bhadlibs.employeepayroll(drop=street:);
run;

Notice variables are dropped in the SET statement (so its not just a SET statement now, it is a SET statement with a drop). The drop=street: (note the colon after street) indicates that all variables whose name begins with street are now dropped.

 

Or to simplify the entire thing into one data step, try this (which will require PROC SORTs before it can work)

 

data mergepart;
    merge bhadlibs.employee_payroll bhadlibs.employee_donations bhadlibs.employee_addresses;
    by id;
    drop street:;
run;

 

--
Paige Miller

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
  • 4 replies
  • 653 views
  • 0 likes
  • 4 in conversation