DATA Step, Macro, Functions and more

Import Excel File- Columns to Multiple observation

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 94
Accepted Solution

Import Excel File- Columns to Multiple observation

I am trying to import excel file to SAS using Proc Import.

There are 3 columns in Excel file.One of the column have different values separated by random delimiters like comma or space or 'and' or double space..seems very random.

 

Given Excel file data

 

ID            Values                                   Date

1             2017138                          2/16/2018

2             2017456 2017778           2/16/2018

3             2017458 and 309878      2/16/2018

4             20178  2078654              2/15/2018

 

 

Output needed:

 

ID             Values                         Date

1              2017138                    2/16/2018

2              2017456                    2/16/2018

2              2017778                    2/16/2018

3               2017458                    2/16/2018

3              309878                      2/16/2018

 

I want like above output,treat the values for ID 2 into two separate observations, Is it possible to create output (multiple observation) like this from a single record during proc import or in Data step/Data step after importing?

 

Thanks for checking!


Accepted Solutions
Solution
‎04-11-2018 09:33 AM
Super User
Posts: 6,939

Re: Import Excel File- Columns to Multiple observation

Posted in reply to Kalai2008

Then you need to know the complete list of values to ignore.  For example:

 

data want;

set imported_data;

newvar = values;

if values=' ' then output;

else do _n_=1 to countw(newvar);

   values = scan(newvar, _n_);

   if values not in ('and') then output;

end;

drop newvar;

run;

 

You can add to the list of values in parentheses (which right now contain just the one item 'and').

View solution in original post


All Replies
Super User
Posts: 6,939

Re: Import Excel File- Columns to Multiple observation

[ Edited ]
Posted in reply to Kalai2008

While you can't do this during the import, a DATA step can handle this pretty well.

 

data want;

set imported_data;

newvar = compress(values, , 'kds');

do _n_=1 to countw(newvar);

   values = scan(newvar, _n_);

   output;

end;

drop newvar;

run;

 

"kds" = keep digits and spaces

Frequent Contributor
Posts: 94

Re: Import Excel File- Columns to Multiple observation

Posted in reply to Astounding

Thank you for reply...My Value column also contain alphabets,alpha/numeric, so the below code truncated it.

Solution
‎04-11-2018 09:33 AM
Super User
Posts: 6,939

Re: Import Excel File- Columns to Multiple observation

Posted in reply to Kalai2008

Then you need to know the complete list of values to ignore.  For example:

 

data want;

set imported_data;

newvar = values;

if values=' ' then output;

else do _n_=1 to countw(newvar);

   values = scan(newvar, _n_);

   if values not in ('and') then output;

end;

drop newvar;

run;

 

You can add to the list of values in parentheses (which right now contain just the one item 'and').

Frequent Contributor
Posts: 94

Re: Import Excel File- Columns to Multiple observation

Posted in reply to Astounding

Thank you so much.. It worked. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 100 views
  • 0 likes
  • 2 in conversation