BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5

data l; infile cards missover truncover; input add $ 1-100; cards; q w e r t y r t t t qwerret w a d f g g run; data l2; set l; if (SCAN(add,5,' ') EQ '' |  LENGTH(COMPRESS(add, ' ') ) <10 | LENGTH(COMPRESS(add, ' ')) >200) then do; add_QC1 =''; add_QC2 =''; add_QC3 = ''; end; else do; add_QC1 = add ; add_QC2 = ''; add_QC3 = '' ; end; run; Actually i am checking if it is having 5 words and length <10 and length greate than 200 i want the output to be : obs add                                  add_QC1 ---  --- 1.  q w e r t y                          q w e r t y 2.  r t t t 3.  qwerret w a d f g g     qwerret w a d f g g               

5 REPLIES 5
R_Win
Calcite | Level 5

you can check the text file for you ref as the above is not fine

Astounding
PROC Star

R_Win,

There are several features of your program to either correct or clarify. 

First, do not use both TRUNCOVER and MISSOVER.  Pick one or the other.  My choice would be neither, but to use:

infile cards lrecl=100 pad;

Since you want to read 100 characters from the data lines, make sure you have 100 characters to read.

Second, you say you want three conditions to be met, using AND to join the three conditions.  But the code uses | which means OR.  I suspect you actually mean OR, since it is not possible for the length to be both less than 10 and greater than 200.  But perhaps it is some combination of AND and OR.  You need to clarify the conditions you want to meet.

Third, since your program reads 100 characters as the value for ADD, it is not possible for the length to be greater than 200.

Finally, the statement add_QC1=''; defines add_QC1 as being 1 character long.  If the later statement executes (add_QC1 = add;), there is not enough room to store the full value of ADD.  Instead, it will be truncated down to a single character.  The easiest solution is to add a LENGTH statement toward the beginning of the DATA step, defining ADD_QC1 as being 100 characters long.

Patch up this much, and we'll see if the questions remain.

Good luck.

R_Win
Calcite | Level 5

if (SCAN(add,5,' ') EQ ' ' Actually i want the info for this is meas it will scan the word having 5 words separated by spaces is it correct

Astounding
PROC Star

R_Win,

That statement is checking that ADD contains fewer than 5 words (using blanks as delimiters).  The SCAN function returns a blank when it searches for a fifth word and doesn't find one.  A few more variations ...

To find ADD with at least 5 words:

if SCAN(add, 5, ' ') > ' ' then ...

To find ADD with exactly 5 words:

if SCAN(add, 5, ' ') > ' ' and scan(add, 6, ' ') = ' ' then ...

Hope this helps.

Ksharp
Super User

if it is having 5 words and length <10 and length greate than 200

Actually,I do not understand what you mean.

Since you want length<10 ,why do you need > 200 ?

data l;
infile cards  truncover;
input add $ 1-100;
cards;
q w e r t y
r t t t t
qwerret w a d f g g
run;
data l2;
 set l;
 length add_QC1 $ 100;
 if countw(add) eq 5 and LENGTH(COMPRESS(add, ' ') ) <10 or  
 LENGTH(COMPRESS(add, ' ')) >200 then add_QC1=' ';
 else add_QC1=add;
run;

Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1016 views
  • 0 likes
  • 3 in conversation