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

Hello:

 

I would like to find 'P' letter in the first position in my dataset 'ID' column.   I would like to use Proc SQL, not data step.   Is there a way to do that?  Thank you.

 

For example, my ID column is list:

 

1. PCR00026

2. NCR00025

3. PCR00027

4. NCR00033

5. NCR00055

 

Therefore, I would like to find the ID #1 and ID#3.

 

proc sql;
	create table Want as
 	select ID 
 	from test
 	where ID....  
 	order by id;
1 ACCEPTED SOLUTION

Accepted Solutions
GPatel
Pyrite | Level 9

DATA A;
INPUT ID $;
CARDS;
PCR00026
NCR00025
PCR00027
NCR00033
NCR00055
;
RUN;
PROC SQL;
CREATE TABLE b AS
SELECT * FROM a
WHERE SUBSTR(ID,1,1)='P'
oRDER BY id;
QUIT;
PROC PRINT;
RUN;

View solution in original post

11 REPLIES 11
GPatel
Pyrite | Level 9

DATA A;
INPUT ID $;
CARDS;
PCR00026
NCR00025
PCR00027
NCR00033
NCR00055
;
RUN;
PROC SQL;
CREATE TABLE b AS
SELECT * FROM a
WHERE SUBSTR(ID,1,1)='P'
oRDER BY id;
QUIT;
PROC PRINT;
RUN;

anushreebiotech
Obsidian | Level 7

Hi,

 

proc sql;
select * from test
where lower(id) like 'p%';
quit;

 

Regards,

Anushree

GPatel
Pyrite | Level 9
PROC SQL;
CREATE TABLE b(where=(SUBSTR(ID,1,1)='P')) AS
SELECT * FROM a
oRDER BY id;
QUIT;

PROC SQL;
CREATE TABLE b AS
SELECT * FROM a(where=(SUBSTR(ID,1,1)='P'))
oRDER BY id;
QUIT;
ybz12003
Rhodochrosite | Level 12

Thank you so much!

FreelanceReinh
Jade | Level 19

The EQT operator is another option:

where ID eqt 'P'
ybz12003
Rhodochrosite | Level 12

That is cool!  How to express not equal?

FreelanceReinh
Jade | Level 19

@ybz12003 wrote:

That is cool!  How to express not equal?


The negative operator corresponding to EQT is the NET operator ("not equal to truncated strings"; see the table I linked in the other post). So, for example, with where name net 'Ja' you could select all names from SASHELP.CLASS not starting with "Ja".

GPatel
Pyrite | Level 9
For “not equal”to operator.
ChrisNZ
Tourmaline | Level 20

The first() function is the appropriate function to use here.

SimonDawson
SAS Employee
I had no idea that function existed! Good suggestion.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 11 replies
  • 2491 views
  • 7 likes
  • 6 in conversation