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 Name | Exchange | Ticker | D-U-N-S# |
Walmart Inc. | NYSE | WMT | 51957769 |
Berkshire Hathaway Inc. | NYSE | BRK.B | 1024314 |
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;
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;
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;
Thank you. I appreciate your quick response. This is an excellent solution!
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!
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.