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

BASUG is hosting free webinars ! Check out recordings of our past webinars: https://www.basug.org/videos. Save the date for our in person SAS Blowout on Oct 18 in Cambridge, MA. Registration opens in September.
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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 501 views
  • 0 likes
  • 4 in conversation