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

Dear community, I am trying to save time. I have Excel Data cleaning work. Unfortunately the data comes all in one column but I can't transpose it simply into rows. I need to extract specific information as in the example want file. Can someone please help.

This is the have file:

Walmart Inc.
NYSE : WMT 
D-U-N-S# 051957769
Berkshire Hathaway Inc.
NYSE : BRK.B 
D-U-N-S# 001024314

 

And this is the want file:

 

Company NameExchangeTickerD-U-N-S#
Walmart Inc.NYSEWMT 51957769
Berkshire Hathaway Inc.NYSEBRK.B 1024314
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
options validvarname=any;
data have;
infile 'c:\temp\have.csv' truncover termstr=lf;
input x $200.;
if not findc(x,':#') then group=0;
group+1;
run;
data temp;
 set have;
 if group=1 then do;n+1;name='CompanyName      ';v=x;output;end;
 else if findc(x,':') then do;
 name='Exchange';v=scan(x,1,':');output;
 name='Ticker';v=scan(x,-1,':');output;
 end;
 else if findc(x,'#') then do;
 name='D-U-N-S#';v=scan(x,-1,' ');output;
 end;
drop x group;
run;
proc transpose data=temp out=want;
by n;
var v;
id name;
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

CSV is not Excel!!  

 

As for the data, if its all in that format, then read line, and retain the variables, something like:

data want;
  infile "have.csv" dlm=",";
  length company exchange ticker duns buff $200;
  retain company exchange ticker duns;
  input buff $;
  if index(buff,":") then do;
    exchange=scan(buff,1,":");
    ticker=scan(buff,2,":");
  end;
  else if substr(buff,1,4)="D-U-" then do;
    duns=scan(buff,2,"#");
    output;
    company="";
    exchange="";
    ticker="";
    duns="";
  end;
  else company=buff;
run;
Agent1592
Pyrite | Level 9
Thank you sir. Excellent solution as well.
Ksharp
Super User
options validvarname=any;
data have;
infile 'c:\temp\have.csv' truncover termstr=lf;
input x $200.;
if not findc(x,':#') then group=0;
group+1;
run;
data temp;
 set have;
 if group=1 then do;n+1;name='CompanyName      ';v=x;output;end;
 else if findc(x,':') then do;
 name='Exchange';v=scan(x,1,':');output;
 name='Ticker';v=scan(x,-1,':');output;
 end;
 else if findc(x,'#') then do;
 name='D-U-N-S#';v=scan(x,-1,' ');output;
 end;
drop x group;
run;
proc transpose data=temp out=want;
by n;
var v;
id name;
run;
Agent1592
Pyrite | Level 9

Thank you. I appreciate your quick response. This is an excellent solution!

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1575 views
  • 3 likes
  • 3 in conversation