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
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
;
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
;
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).
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
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):
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.
@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.
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.