BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
twildone
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
dsbihill
Obsidian | Level 7

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

10 REPLIES 10
Jagadishkatam
Amethyst | Level 16

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
data_null__
Jade | Level 19

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.

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
twildone
Pyrite | Level 9

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;

Jagadishkatam
Amethyst | Level 16

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
data_null__
Jade | Level 19

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.

dsbihill
Obsidian | Level 7

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, ',')

Tom
Super User Tom
Super User

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,|) ...

dsbihill
Obsidian | Level 7

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;

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!

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
  • 10 replies
  • 1334 views
  • 6 likes
  • 7 in conversation