BookmarkSubscribeRSS Feed
mauri0623
Quartz | Level 8

I have the followings,

*--FA807518F1456-FA807518F1456-1002-07-HS002193888 
*--HS002108475----HS002108475 
*---HS002105485---SFHS002105485 
*---HS002105485---SFHS002105485 
*---HS002105485---SFHS002105485 
*---HS002105485---SFHS002105485 
*------SFHS002106080 
*------AAZKA021821TSPLOS 
*------AAZKA021821TSPBRK 
*------AAZKA030421TSPLOS 
*------AAZKA030421TSPBRK 
*------AAZKA031821TSPBRK 
*--HQ0642149245----HQ0642149245 
*------AAZKA040121TSPLOS 
*------AAZKA041221TSPBRK 
*---HS47371659Z010---HS47371659Z010 
*--HQ0642149245----HQ0642149245 
*--AGENTPAGE----HS002121076MW0 
*--ZKA----AA 
*--ZKA----AA 
*--AGENTGUZMAN----HS002121076MW0 
*--AGENTLEE----HS002121076MW0 
*--AGENTMELLGREN2----HS002121076MW0 
*--AGENTPRICE----HS002121076MW0 
*--PWFFSA21337860321--0007--HAFC0583142 
*--AGENTELDRIDGE----HS002121076MW0 
*--AGENTMELLGREN----HS002121076MW0 
*------AAZKA042921TSPBRK 
*--N0003421----AA 

I am trying to get those rows that start with HS002 or ZKA and place them in a variable called GOTIT. Some kind of substring. Thank you!
10 REPLIES 10
japelin
Rhodochrosite | Level 12

What does the "*--" mean?
Can you describe the image of the dataset you want?

mauri0623
Quartz | Level 8
That is a part of the string. I want the HS002 rows.
japelin
Rhodochrosite | Level 12

I'm sorry to say that "*--HS002" does not begin with "HS002"... but is this the result you want?

 

2021-06-23_00h00_08.png

Cynthia_sas
SAS Super FREQ
Hi:
What about the "ZKA" requirement. I see rows that have --ZKA-- and I see rows that have AAZKA. Also, do you want a separate dataset with ONLY the HS002 and ZKA rows or do you want to keep the original data and create a new variable called GOTIT?
Cynthia
mauri0623
Quartz | Level 8
Only HS002 and ZKA.
Cynthia_sas
SAS Super FREQ

Hi:

  But those strings appear multiple places, as shown below. The WHERE_HS variable shows the place in the string where HS002 is found and the WHERE_ZK shows the place in the string where ZKA is found. Do you want ALL of those rows? Or are there more criteria?

Cynthia

These are all the rows that have HS002 or ZKA:

Cynthia_sas_0-1624376272956.png

  You initially listed 29 rows and 24 rows met the simple condition of having either HS002 or ZKA in the string.

 

 

mauri0623
Quartz | Level 8
*--HS002108475----HS002108475
*---HS002105485---SFHS002105485
*---HS002105485---SFHS002105485
*---HS002105485---SFHS002105485
*---HS002105485---SFHS002105485
*--ZKA----AA
*--ZKA----AA
only these one.
Cynthia_sas
SAS Super FREQ
HI:
Thanks for the clarification. What code have you tried? Based on a quick test, it seems like the INDEX function would work for you.
Cynthia
mauri0623
Quartz | Level 8
I tried index but the values are coming up as numbers.


ballardw
Super User

@mauri0623 wrote:
I tried index but the values are coming up as numbers.



Of course they are. Index returns a position number of where the value is found and 0 when not found.

So typically if the criteria is "somevalue somewhere in the variable" then code often looks like:

If index(variable,'somevalue') > 0 then <do something>;

where the "do something" could be assign a value to a variable, delete the record, start a DO/End block of code to do something more complex.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1330 views
  • 0 likes
  • 4 in conversation