BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gil_
Quartz | Level 8
Hi I have a data set looks like this
Date Tracking. Col1 col2 col3 col4 col5 col6.col7 col8
01/01/16 2z. $$ bag. Atm. { BAG. STM $$ {
I need to remove the cold where the symbols $$ or { exist and just keep where bag and atm exist.
1 ACCEPTED SOLUTION

Accepted Solutions
rogerjdeangelis
Barite | Level 11
Drop columns that contain '$$' or '('

inspired by
https://goo.gl/2GydUH
https://communities.sas.com/t5/Base-SAS-Programming/Remove-columns-with-characters/m-p/335357

HAVE
====

Up to 40 obs WORK.HAVE total obs=1

Obs      DATE      TRACKING    COL1    COL2    COL3    COL4    COL5    COL6    COL7    COL8

 1     01/01/16      2z.        $$     bag.    Atm.     {      BAG.    STM      $$      {

WANT
====

Up to 40 obs WORK.WANT total obs=1

bs      DATE      TRACKING    COL2    COL3    COL5    COL6

1     01/01/16      2z.       bag.    Atm.    BAG.    STM

DETAILS
=======

  COL1='$$' so drop
  COL7='$$' so drop

WORKING CODE
============

      rc=dosubl('
       if chrs[i]="$$" then rol=catx(" ",rol,vname(chrs[i]));
       call symputx("drp",rol);

      set have(drop=&drp);


FULL SOLUTION
=============

Create some data;
data have;
input (Date  Tracking Col1 col2 col3 col4 col5 col6 col7 col8) ($);
cards4;
01/01/16 2z. $$ bag. Atm. { BAG. STM $$ {
;;;;
run;quit;

%symdel drp;
data want;
 if _n_=0 then do;
   rc=%sysfunc(dosubl('
    data out;
       length rol $200;
       do until (dne);
          set have end=dne;
          array chrs[*]  col:;
          do i=1 to dim(chrs);
            put chrs[i] ;
            if chrs[i]="$$" or chrs[i]="{" then rol=catx(" ",rol,vname(chrs[i]));
          end;
       end;
       call symputx("drp",rol);
       stop;
    run;quit;
 '));
 end;

 set have(drop=&drp);
 drop rc;

run;quit;

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

Please show data before and after.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Gil_
Quartz | Level 8
Ok
date. Tracking col1. Col2. Col3 col4. Col5 col6 col7
01/01/16. 1z. $$ bag. Atm $$ { bag atm
Output

Date. Tracking col2 col3. Col6. col7
01/01/26. 1 z Bag. Atm. Bag. Atm
The bag has 7 characters atm has 7 character after the word bag atm ..thanks for quick response


Gil_
Quartz | Level 8
Fyi the file is a csv
art297
Opal | Level 21

What you have and want isn't clear from what you've shown. Provide the have and want example in the form of two data steps.

 

Art, CEO, AnalystFinder.com

 

rogerjdeangelis
Barite | Level 11
Drop columns that contain '$$' or '('

inspired by
https://goo.gl/2GydUH
https://communities.sas.com/t5/Base-SAS-Programming/Remove-columns-with-characters/m-p/335357

HAVE
====

Up to 40 obs WORK.HAVE total obs=1

Obs      DATE      TRACKING    COL1    COL2    COL3    COL4    COL5    COL6    COL7    COL8

 1     01/01/16      2z.        $$     bag.    Atm.     {      BAG.    STM      $$      {

WANT
====

Up to 40 obs WORK.WANT total obs=1

bs      DATE      TRACKING    COL2    COL3    COL5    COL6

1     01/01/16      2z.       bag.    Atm.    BAG.    STM

DETAILS
=======

  COL1='$$' so drop
  COL7='$$' so drop

WORKING CODE
============

      rc=dosubl('
       if chrs[i]="$$" then rol=catx(" ",rol,vname(chrs[i]));
       call symputx("drp",rol);

      set have(drop=&drp);


FULL SOLUTION
=============

Create some data;
data have;
input (Date  Tracking Col1 col2 col3 col4 col5 col6 col7 col8) ($);
cards4;
01/01/16 2z. $$ bag. Atm. { BAG. STM $$ {
;;;;
run;quit;

%symdel drp;
data want;
 if _n_=0 then do;
   rc=%sysfunc(dosubl('
    data out;
       length rol $200;
       do until (dne);
          set have end=dne;
          array chrs[*]  col:;
          do i=1 to dim(chrs);
            put chrs[i] ;
            if chrs[i]="$$" or chrs[i]="{" then rol=catx(" ",rol,vname(chrs[i]));
          end;
       end;
       call symputx("drp",rol);
       stop;
    run;quit;
 '));
 end;

 set have(drop=&drp);
 drop rc;

run;quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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