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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

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