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
Onyx | Level 15
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
Onyx | Level 15
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!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 446 views
  • 0 likes
  • 2 in conversation