BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
POOJA_J
Obsidian | Level 7

POOJA_J_0-1697593798413.png

I supposed the answer to be Buenos Aires.

But I get Buenos Aires, Argentina as correct ans.

Plz explain how the this works.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Please always share code as text and not as screenshot.

 

%let location=%str(Buenos Aires, Argentina);
%let city=%scan(&location, 1, %str(,));
%put &=city;

The documentation tells you that %scan() allows for FOUR parameters.

Patrick_0-1697594687725.png

The last comma in your code is not interpreted as the delimiter for the charlist but as separator for the next parameter modifier. This means that you define the value for charlist blank meaning there isn't any.

 

To get what you want quote the last comma so it gets used as value for charlist.

%let location=%str(Buenos Aires, Argentina);
%let city=%scan(&location, 1, %str(,));
%put &=city;

 

 

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

Please always share code as text and not as screenshot.

 

%let location=%str(Buenos Aires, Argentina);
%let city=%scan(&location, 1, %str(,));
%put &=city;

The documentation tells you that %scan() allows for FOUR parameters.

Patrick_0-1697594687725.png

The last comma in your code is not interpreted as the delimiter for the charlist but as separator for the next parameter modifier. This means that you define the value for charlist blank meaning there isn't any.

 

To get what you want quote the last comma so it gets used as value for charlist.

%let location=%str(Buenos Aires, Argentina);
%let city=%scan(&location, 1, %str(,));
%put &=city;

 

 

POOJA_J
Obsidian | Level 7
Thanks for explaining. I have further question.
While using Scan function when we don't mention the delimiter, it considers all delimiters. The %scan won't behave the same? I mean suppose I don't mention the delimiter in %scan should it give ans as Buenos?
Patrick
Opal | Level 21

If you don't specify explicit characters as delimiters then the default list will get used. This applies both for scan() and %scan() and is fully documented.

Patrick_0-1697596444577.png

 

 

Tom
Super User Tom
Super User

But you did include it. 

Just try it without the extra commas to get the default delimiters.

1    %let location=%str(Buenos Aires, Argentina);
2    %put city=%scan(&location, 1, %str(,));
city=Buenos Aires
3    %put city=%scan(&location, 1);
city=Buenos
4    %put city=%scan(&location, 1,);
city=Buenos Aires, Argentina
5    %put city=%scan(&location, 1,,);
city=Buenos Aires, Argentina

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1154 views
  • 3 likes
  • 3 in conversation