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;
Please show data before and after.
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.