DATA Step, Macro, Functions and more

Scan for carriage return

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Scan for carriage return

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)


Accepted Solutions
Solution
‎12-30-2016 04:10 AM
PROC Star
Posts: 7,363

Re: Scan for carriage return

[ Edited ]

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

 

View solution in original post


All Replies
Solution
‎12-30-2016 04:10 AM
PROC Star
Posts: 7,363

Re: Scan for carriage return

[ Edited ]

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

 

Occasional Contributor
Posts: 9

Re: Scan for carriage return

Awesome, nice thinking. 

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 207 views
  • 1 like
  • 2 in conversation