BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
tinton
Fluorite | Level 6
PROC SQL; CREATE TABLE V1 AS SELECT * FROM ACTUAL WHERE SUBSTR(MFCUST14,-3,3) IN ('HCI','CIW','HFI','FIW);

Why there's empty output eventhough MFCUST14=M9005FIW is in the table?
1 ACCEPTED SOLUTION

Accepted Solutions
SAS76FR
Fluorite | Level 6

 

I think  SUBSTR(MFCUST14,-3,3) is empty 

 

try this 

PROC SQL; CREATE TABLE V1 AS SELECT *  FROM ACTUAL WHERE SUBSTR(MFCUST14,length(MFCUST14)-2,3) IN ('HCI','CIW','HFI','FIW');quit;

 

 

View solution in original post

3 REPLIES 3
tinton
Fluorite | Level 6
It's okay, I've figure it out. Just use Select * from actual where mfcust14 like '%HFI';
SAS76FR
Fluorite | Level 6

 

I think  SUBSTR(MFCUST14,-3,3) is empty 

 

try this 

PROC SQL; CREATE TABLE V1 AS SELECT *  FROM ACTUAL WHERE SUBSTR(MFCUST14,length(MFCUST14)-2,3) IN ('HCI','CIW','HFI','FIW');quit;

 

 

MayurJadhav
Quartz | Level 8

I believe you want to read last three characters from the variable MFCUST14 and compare it with ('HCI','CIW','HFI','FIW). If this is true then you need to calculate length of the string first and then subtract -2 from it. The strip function used here to remove all leading or trailing blanks from a variable MFCUST14.

 

The below code will give you better understanding:

 

data _null_;
	MFCUST14='M9005FIW';
	sub_str=SUBSTR(strip(MFCUST14), length(MFCUST14)-2, 3);
	put MFCUST14=;
	put sub_str=;
run;

Log:

69 data _null_;
70 MFCUST14='M9005FIW';
71 sub_str=SUBSTR(strip(MFCUST14), length(MFCUST14)-2, 3);
72 put MFCUST14=;
73 put sub_str=;
74 run;
MFCUST14=M9005FIW
sub_str=FIW

 

If this is your objective then you need to adjust your proc sql code accordingly. But I'd suggest using data step instead of a proc sql.

 

data actual;
	MFCUST14='M9005FIW';
	output;
	MFCUST14='M9005FI3';
	output;
	MFCUST14='M9CIW';
	output;
	MFCUST14='M9005XYZ';
	output;
run;

data V1;
	set ACTUAL;

	if SUBSTR(MFCUST14, length(MFCUST14)-2, 3) IN ('HCI', 'CIW', 'HFI', 'FIW') then
		output;
run;

 

Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 391 views
  • 1 like
  • 3 in conversation