DATA Step, Macro, Functions and more

How to know weather the data set contains special characters in each variable or not

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to know weather the data set contains special characters in each variable or not

Hi,

 

data ds;

infile datalines;

input a$ b$ c$ d$ e$;

datalines;

1 2 as> sd< 78#

3 5 a>a sd< 78&

1 2 <ss sd< 78*

1 2 ss 3 84

;

run;

 

output may be like 

_n_

1

2

3

 

or

 

1 2 as> sd< 78#

3 5 a>a sd< 78&

1 2 <ss sd< 78*

 

 

I was trying

 

data ds1;
array spc(93) _All_;
set work.ds nobs=nobs point=i;
do i=1 to dim(spc);
where spc(i) like (<%,>%,%>%,>%,%>,%>%);
end;
stop;
run;

 

 

Thanks in advance

 

 

 


Accepted Solutions
Solution
‎05-18-2018 05:45 AM
Respected Advisor
Posts: 4,797

Re: How to know weather the data set contains special characters in each variable or not

Posted in reply to srinath3111

@srinath3111

You need to provide a bit more explanation in your posts. Don't forget that you're asking us to help you so please have the courtesy and spend the time required to help us to help you.

 

Following the coding approach you've tried below code should do the job.

data have;
  infile datalines;
  input a$ b$ c$ d$ e$;
  datalines;
1 2 as> sd< 78#
3 5 a>a sd< 78&
1 2 <ss sd< 78*
1 2 ss 3 84
;
run;

data want(drop=_:);
  set work.have;
  array spc(*) _character_;
  do _i=1 to dim(spc);
    if notalnum(compress(spc[_i])) then
      do;
        row=_n_;
        output;
        leave;
      end;
  end;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,840

Re: How to know weather the data set contains special characters in each variable or not

Posted in reply to srinath3111

Some information would be helpful.  Special characters normally refer to tabs, or carriage return etc.  I don't see any of these here?

Do you just want to see if the string contains <, >, or %?  If so:

data ds1;
  array spc{93} _all_;
  set work.ds;
  do i=1 to dim(spc);
    if lengthn(compress(spc{i},'<>%',"k")) then k=1;
  end;
  if k;
run;

This goes over each variable, if the string when all but <>% are removed's length is greater than 0 then k is set to 1.  After all array elements are checked, if k is 1 then it gets output.  

Contributor
Posts: 40

Re: How to know weather the data set contains special characters in each variable or not

Hi,

 

It doesnt work.

 

Thanks

Super User
Posts: 13,941

Re: How to know weather the data set contains special characters in each variable or not

Posted in reply to srinath3111

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Solution
‎05-18-2018 05:45 AM
Respected Advisor
Posts: 4,797

Re: How to know weather the data set contains special characters in each variable or not

Posted in reply to srinath3111

@srinath3111

You need to provide a bit more explanation in your posts. Don't forget that you're asking us to help you so please have the courtesy and spend the time required to help us to help you.

 

Following the coding approach you've tried below code should do the job.

data have;
  infile datalines;
  input a$ b$ c$ d$ e$;
  datalines;
1 2 as> sd< 78#
3 5 a>a sd< 78&
1 2 <ss sd< 78*
1 2 ss 3 84
;
run;

data want(drop=_:);
  set work.have;
  array spc(*) _character_;
  do _i=1 to dim(spc);
    if notalnum(compress(spc[_i])) then
      do;
        row=_n_;
        output;
        leave;
      end;
  end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 122 views
  • 0 likes
  • 4 in conversation