BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raja777pharma
Fluorite | Level 6

Hello ,

I have tried proc import with getnames=Yes|No , but couldn't find a right way do it below scenario.

 

When Excel column name ( A,B,C,D -n) and  variables  names (A1,B1,C1,D1-n1) are same then those columns are need to drop in sas data set output

 

Example: In Below screen shot of excel sheet there  are five columns (A-E) and i want in output sas data set only columns A,B and C .

 

output  data set variables should be  SUBJID,STUDY and DATE only 

this example of excel sheet, for my data excel sheet have more columns and randomly  the varibles names are same as excel columns names so need to drop the same columns when proc import

 

raja777pharma_0-1597308868052.png

 

Thank you,

Raj

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Create a reference table that connects variable position in a dataset with the respective default Excel column name:

data excel_cols;
length name $32;
varnum = 0;
do i = 65 to 65 + 25;
  varnum + 1;
  name = byte(i);
  output;
end;
do i = 65 to 65 + 25;
  do j = 65 to 65 + 25;
    varnum + 1;
    name = byte(i) !! byte(j);
    output;
  end;
end;
run;

This will give you the names from A to ZZ (more than 700 columns, should suffice).

Then you join this with the metadata of your imported table:

proc sql noprint;
select d.name
  into :drop_cols separated by " "
  from dictionary.columns d right join excel_cols c
  on d.name = c.name and d.varnum = c.varnum
  where d.libname = "WORK" and d.memname="TAB1"
;
quit;

%put &drop_cols;

The macro variable will contain the column names of the typical "unnamed" columns in the spreadsheet. You can use it in a DROP statement or DROP= dataset option.

 

I tested this with an import of an export of sashelp.class where I removed the headers for weight and height in the spreadsheet (see attachment), and it gave me

D E

as result, as it should be.

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

Have you tried using the keep-option:

proc import .... data=work.import(keep=SubjId Study Date) ...
Kurt_Bremser
Super User

Create a reference table that connects variable position in a dataset with the respective default Excel column name:

data excel_cols;
length name $32;
varnum = 0;
do i = 65 to 65 + 25;
  varnum + 1;
  name = byte(i);
  output;
end;
do i = 65 to 65 + 25;
  do j = 65 to 65 + 25;
    varnum + 1;
    name = byte(i) !! byte(j);
    output;
  end;
end;
run;

This will give you the names from A to ZZ (more than 700 columns, should suffice).

Then you join this with the metadata of your imported table:

proc sql noprint;
select d.name
  into :drop_cols separated by " "
  from dictionary.columns d right join excel_cols c
  on d.name = c.name and d.varnum = c.varnum
  where d.libname = "WORK" and d.memname="TAB1"
;
quit;

%put &drop_cols;

The macro variable will contain the column names of the typical "unnamed" columns in the spreadsheet. You can use it in a DROP statement or DROP= dataset option.

 

I tested this with an import of an export of sashelp.class where I removed the headers for weight and height in the spreadsheet (see attachment), and it gave me

D E

as result, as it should be.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 6584 views
  • 0 likes
  • 3 in conversation