DATA Step, Macro, Functions and more

re: Scan function

Accepted Solution Solved
Reply
Regular Contributor
Posts: 222
Accepted Solution

re: Scan function

Hi....I would like to be able to select a ID one at a time from a list. Each ID consist of 3 letters followed by a space and 4 digit number. I need to keep this ID in this form. I am assume if I used the scan function, it would consider each ID as 2 separate ID's because of the blank space in between them. Is there a way around this. Thanks,

ID

CME 1199

DLS 1213

WRF 1155

RTY 1345


Accepted Solutions
Solution
‎06-15-2015 12:38 PM
Contributor
Posts: 53

Re: re: Scan function

If you are trying to handle both situations of 3 letters and no letters why not change your above code to something like this

PROC SQL NOPRINT;

     SELECT DISTINCT ID 

           INTO :IDLIST1 SEPARATED BY ','

     FROM have;

QUIT;

and then use on the macro side

%scan(&idlist1, i, ',')

View solution in original post


All Replies
Trusted Advisor
Posts: 1,130

Re: re: Scan function

Please try

data have;

  input text &$ 20.;

cards;

CME 1199

DLS 1213

WRF 1155

RTY1345

;

data want;

set have;

if prxmatch('/\w\w\w\D\S\d+/',text) then flag=1;

run;

Thanks,

Jag

Thanks,
Jag
Respected Advisor
Posts: 3,777

Re: re: Scan function

If your delimited list contains items that contain the delimiter the item must mask the delimiter in some way.  Typically this is done by enclosing the item(s) that contain delimiters in quotes.

Super Contributor
Super Contributor
Posts: 3,174

Re: re: Scan function

If your list originates from a vertical (separate records) as you have illustrated, then consider using a different item separator character (one that does not appear within any item).  So if you are using PROC SQL to generate a macro-variable as your item-list, then choose a different  SEPARATED BY data-string value to distinguish items in your list, then use that data-string as your SCAN argument when parsing.

Scott Barry

SBBWorks, Inc.

Trusted Advisor
Posts: 1,615

Re: re: Scan function

twildone wrote:

Hi....I would like to be able to select a ID one at a time from a list. Each ID consist of 3 letters followed by a space and 4 digit number. I need to keep this ID in this form. I am assume if I used the scan function, it would consider each ID as 2 separate ID's because of the blank space in between them. Is there a way around this. Thanks,

ID

CME 1199

DLS 1213

WRF 1155

RTY 1345

I don't understand the reason why SCAN is suggested here. You don't need SCAN to select from the list. Could you explain further?

Regular Contributor
Posts: 222

Re: re: Scan function

Hi...I was using the scan function as part of a macro loop to check to see whether or not the list of ID's had records contained in a larger dataset when the ID was only numerical (example, 1199) Now the ID field has changed to include 3 letters followed by a blank space and now I need to do the same check but now with the 3 letters preceeding the 4 numbers....

PROC SQL NOPRINT;

     SELECT DISTINCT ID  

           INTO :IDLIST1 SEPARATED BY ' '

     FROM WORK.ID_LIST;

QUIT;

Trusted Advisor
Posts: 1,130

Re: re: Scan function

since you are trying to search the id which have 3 letters followed by space and 4 digits.  please try

PROC SQL NOPRINT;

     SELECT DISTINCT ID  

           INTO :IDLIST1 SEPARATED BY ' '

     FROM WORK.ID_LIST where prxmatch('/\w\w\w\D\S\d\d\d/',text)>0;

QUIT;


Thanks,

Jag

Thanks,
Jag
Respected Advisor
Posts: 3,777

Re: re: Scan function

Why use macro at all?.  Just get the distinct ID list and do the test using SQL or data step.  Doing an operation on records from a SAS data set is much easier than fiddling with scan the values of a macro variable.

Solution
‎06-15-2015 12:38 PM
Contributor
Posts: 53

Re: re: Scan function

If you are trying to handle both situations of 3 letters and no letters why not change your above code to something like this

PROC SQL NOPRINT;

     SELECT DISTINCT ID 

           INTO :IDLIST1 SEPARATED BY ','

     FROM have;

QUIT;

and then use on the macro side

%scan(&idlist1, i, ',')

Super User
Super User
Posts: 6,500

Re: re: Scan function

Use a delimiter that is not part of the data.  For example |.

The third parameter to the SCAN() or %SCAN() function is the delimiter value.

.... separated by '|' ....

... %scan(&idlist,&i,|) ...

Contributor
Posts: 53

Re: re: Scan function

If i understand your question properly this is how i would do it.  Im sure there are more elegant ways to do this.

Thanks

data have;

input id $50.;

datalines;

CME 1199 DLS 1213 WRF 1155 RTY 1345

;

run;

data want;

    set have;

    do i=1 to countw(id) by 2;

        sep_id = catx(' ',scan(id,i), scan(id, i+1));

        output;

    end;

    drop id i;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 358 views
  • 6 likes
  • 7 in conversation