I have a list of user entered data from xls which I have read into SAS, one of the fields has multiple entries, which the below codes scans and creates a new lookup table for all the different codes entered into a more manageable one to many table, the fields are separated by different characters including; space, comma, semi colon etc. The below code handles them fine, however I want to also scan by carraige return / new line. How do I add that to my scan?
Thanks.
data product_list;
set products(keep=id product_codes);
count=countw(product_codes);
do i = 1 to count;
product_code = scan(product_codes, i, ", .;");
output;
end;
drop count i product_codes;
run;
Guide Version: 5.1 (5.100.0.12019) (64-bit)
One way is to convert the line feeds using the translate function in a data step. Whether they will be '0D'x or '0A'x is dependent upon your computer's operating system, but you can include both to make sure. Nothing has to be changed to the code you posted. e.g.:
proc import datafile="c:\art\testfile.xlsx"
out=products replace dbms=xlsx;
getnames=yes;
sheet='Sheet1';
run;
data products;
set products;
product_codes=translate(product_codes,',','0A'x,'0D'x);
run;
data product_list;
set products(keep=id product_codes);
count=countw(product_codes);
do i = 1 to count;
product_code = scan(product_codes, i, ", .;");
output;
end;
drop count i product_codes;
run;
Art, CEO, AnalystFinder.com
One way is to convert the line feeds using the translate function in a data step. Whether they will be '0D'x or '0A'x is dependent upon your computer's operating system, but you can include both to make sure. Nothing has to be changed to the code you posted. e.g.:
proc import datafile="c:\art\testfile.xlsx"
out=products replace dbms=xlsx;
getnames=yes;
sheet='Sheet1';
run;
data products;
set products;
product_codes=translate(product_codes,',','0A'x,'0D'x);
run;
data product_list;
set products(keep=id product_codes);
count=countw(product_codes);
do i = 1 to count;
product_code = scan(product_codes, i, ", .;");
output;
end;
drop count i product_codes;
run;
Art, CEO, AnalystFinder.com
Awesome, nice thinking.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.