DATA Step, Macro, Functions and more

Datalines error

Accepted Solution Solved
Reply
Super Contributor
Posts: 394
Accepted Solution

Datalines error

Hello:

 

I found a problem when I try to create SAS dataset.   I would like to add "TOF +/- PA, DORV -TGA" into column Analysis.  But the result only shows "TOF +/- PA", please advice how to fix it.  Thanks.

 

 

data test1;

infile datalines dsd;

input Author : $20. Analysis : $100. Product : $100.;

datalines;

 

Alissa, Cleft Lip +/- Palate, LABETOLOL,

Marilyn, TOF +/- PA, DORV -TGA, BUTALBITAL,

 

;

 


Accepted Solutions
Solution
‎01-24-2018 02:50 PM
Super User
Posts: 6,648

Re: Datalines error

Using DSD tells SAS to use commas as a delimiter.  Commas mark the end of a variable.  You can't have it both ways and expect that commas sometimes are taken to be text.  They either mark the end of a variable or they don't.

 

If you want commas to be text, you can change the commas between fields to something else such as | and replace dsd with dlm='|'

View solution in original post


All Replies
PROC Star
Posts: 1,605

Re: Datalines error

did it in a hurry, but you can organize it better or you would at-least get the idea


 data test1;
infile datalines dsd truncover col=d;
input Author : $20. @;
_k=length(strip(scan(_infile_ ,-2,',','M' )));
_fwidth=length(strip(_infile_))-_k-d-1;
input  @(d+1) Analysis  $varying25. _fwidth product : $100.;
drop _:;
datalines;
Alissa, Cleft Lip +/- Palate, LABETOLOL,
Marilyn, TOF +/- PA, DORV -TGA, BUTALBITAL,
;
Solution
‎01-24-2018 02:50 PM
Super User
Posts: 6,648

Re: Datalines error

Using DSD tells SAS to use commas as a delimiter.  Commas mark the end of a variable.  You can't have it both ways and expect that commas sometimes are taken to be text.  They either mark the end of a variable or they don't.

 

If you want commas to be text, you can change the commas between fields to something else such as | and replace dsd with dlm='|'

Trusted Advisor
Posts: 1,831

Re: Datalines error

[ Edited ]

You better define a dofferent delimiter between the variables, such as '#' assuming is not in any variable data:

 

data test1;

infile datalines dlm='#';

input Author : $20. Analysis : $100. Product : $100.;

datalines;

 

Alissa # Cleft Lip +/- Palate # LABETOLOL,

Marilyn # TOF +/- PA, DORV -TGA # BUTALBITAL,

 

;


 
Super Contributor
Posts: 394

Re: Datalines error

Thanks, Shmuel.   Your codes works too!

Super Contributor
Posts: 394

Re: Datalines error

Thanks for everyone's quick response.   I picked the easiest way. Woman Happy

PROC Star
Posts: 1,605

Re: Datalines error

@ybz12003, i wonder whether changing the raw file structure the most easy or convenient thing to do.  I thought you were after how to deal with embedded delimiter ',' in a comma separated file which gives rise to the challenge of using infile options and play with input buffer columns. 

Anyway, If you are so comfortable with the former, all you need to do is wrap your values with quotes  to make the code you wrote to work

 

/*so no changes in your code, just the quotes " around values */ 
data test1;

infile datalines dsd;

input Author : $20. Analysis : $100. Product : $100.;

datalines;
"Alissa", "Cleft Lip +/- Palate", "LABETOLOL",
"Marilyn", "TOF +/- PA, DORV -TGA", "BUTALBITAL",
;

Super Contributor
Posts: 394

Re: Datalines error

Posted in reply to novinosrin

Thank you so much, Novinosrin.   That works too!

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 152 views
  • 3 likes
  • 4 in conversation