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

Below is the program:

 

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size dirname $1000;
input Size dirname $;
datalines;
21561 /Folder_no_space/Folder_no_space/Folder with Space/Folder_no_space/file.sas7bdat
;
run;

 

Problem: Since there is a ' ' in variable dirname, what will only be read are those before ' '. Is there a way to fix this? What I want is for dirname to read even ' '

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Read size as a number, and use the && modifier:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21561 /Folder_no_space/Folder_no_space/Folder with Space/Folder_no_space/file.sas7bdat
;

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

Read size as a number, and use the && modifier:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21561 /Folder_no_space/Folder_no_space/Folder with Space/Folder_no_space/file.sas7bdat
;
iSAS
Quartz | Level 8
Thank you for helping. I just have a follow-up question though. How come if my variable dirname have more than 2 spaces, it doesn't read all of it anymore? Below is the code:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More Spaces/Folder_no_space/file.sas7bdat
;
run;

May I know how to fix it? Thanks
Kurt_Bremser
Super User

I see no problem:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More Spaces/Folder_no_space/file.sas7bdat
;

proc print data=sample noobs;
run;

Result:

Size	dirname
21560	/Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561	/Folder_no_space/Folder_no_space/Two Spaces/Folder_no_space/file.sas7bdat
21562	/Folder_no_space/Folder_no_space/More Spaces/Folder_no_space/file.sas7bdat

Please use the "little running man" button for posting code, as the main posting window will reformat your text along HTML conventions (compressing all whitespace to a single blank, for instance).

iSAS
Quartz | Level 8

Sending the code the have issues when dirname has more then 1 space:
data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two  Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More                                     Spaces/Folder_no_space/file.sas7bdat
;
run;

Result:
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two
21562 /Folder_no_space/Folder_no_space/More

Kurt_Bremser
Super User

Then you need to have quotes around the strings, or use a delimiter other than blank. The ?? works only as long as single blanks are contained in the strings.

And use the "little running man" button for posting code, it is the one right next to the one indicated (which is for logs and other formatted text data):

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

Astounding
PROC Star

Here's a promising approach:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size @;
dirname = substr(_infile_, 7);
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two  Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More                                     Spaces/Folder_no_space/file.sas7bdat
;

Even if your real life situation is more complex, these tools should be adaptable to what you are facing.  For example, you may need to locate the position of ".sas7bdat" in order to limit what SUBSTR selects.

ballardw
Super User

@iSAS wrote:
Thank you for helping. I just have a follow-up question though. How come if my variable dirname have more than 2 spaces, it doesn't read all of it anymore? Below is the code:

data sample;
infile datalines truncover dlmstr=' ' dsd;
length Size 8 dirname $1000;
input Size dirname &&:$1000.;
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More Spaces/Folder_no_space/file.sas7bdat
;
run;

May I know how to fix it? Thanks

From the documentation on INFILE DSD option:

...

When you specify DSD, SAS treats two consecutive delimiters as a missing value

So if your character value has two blanks you tell SAS that you have ended the first value by including them.

If you must use spaces in the values then you need to find either a better delimiter or change how you read the data, perhaps fixed columns.

 

Ksharp
Super User
data sample;
infile datalines truncover dlmstr=' ' dsd;
input Size dirname $1000.;
datalines;
21561 /Folder_no_space/Folder_no_space/Folder with Space/Folder_no_space/file.sas7bdat
;
run;
novinosrin
Tourmaline | Level 20

Hi @iSAS  just an in-formatted input should do?

 



data sample;
infile datalines truncover ;
input Size dirname $1000.;
datalines;
21560 /Folder_no_space/Folder_no_space/One Space/Folder_no_space/file.sas7bdat
21561 /Folder_no_space/Folder_no_space/Two Spaces/Folder_no_space/file.sas7bdat
21562 /Folder_no_space/Folder_no_space/More Spaces/Folder_no_space/file.sas7bdat
;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1860 views
  • 4 likes
  • 6 in conversation