BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10

Hey Folks,

I have this data set.

quoted_string.JPG

As you can see, in the column "rawdata" is a string which consists of double quoted substrings, which are separated by a blank. 

 

I wanto to extract the data fields, i.e. the double quoted substrings and write them into variables, however SAS does a "wrong" counting:

blind_to_quotes.JPG

Lines/Observations 1 to 4 are correct, but lines/observations 5 to 10 are wrong in the sense, that they are all together one substring, because it is quoted in one pair of double quotes!. Also lines 11 and 12 are wrong, as SAS should recognise, that it is a NULL/empty substring.

 

My code that got me the above screenshot:

data test2;
set work.test1;
do i=1 to countw(rawdata);
a = scan(rawdata,i) ;
output;
end;
run;

Obviously, the the result of coundw(rawdata) ist 12 (substrings) rather than 6, which would be correct.

Any ideas in what way I have to modify the countw - function, in order to get the correct number of substrings?

Cheers,

FK1

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16
data test2;
  rawdata = '"a" "b c" "d"';

  do i=1 to countw(rawdata, " ", "Q");
    a = scan(rawdata,i," ","Q") ;
    output;
  end;
run;

I would start with reading doc about options in countw() and scan().

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p18xi2516ihygyn1qg1b1nby326k.h...

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Amethyst | Level 16
data test2;
  rawdata = '"a" "b c" "d"';

  do i=1 to countw(rawdata, " ", "Q");
    a = scan(rawdata,i," ","Q") ;
    output;
  end;
run;

I would start with reading doc about options in countw() and scan().

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p18xi2516ihygyn1qg1b1nby326k.h...

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10
Perfect, yabwon!
I did read through the doc of the "countw" function but obviously not thoroughly enough.

Thank you, for helping out!

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
  • 2 replies
  • 754 views
  • 0 likes
  • 2 in conversation