While Import excel file using Proc Import for few subject# ID I am getting some specific character value. Example below.
"
3-158-021" Expecting Subject id should be: 3-158-021 (9 characters)
The below SAS code I have used however, it is not working. any help would be appreciated.
data Keosys_vdr2;
length SubjectNumber $150;
set Keosys_vdr1 (firstobs=2);
subjectnumber=strip(compress(Subject,'"'));
*subjectnumber1=prxchange('s/\"//',-1,subjectnumber);
keep SiteNumber subjectnumber subjectnumber1 Current_Status;
run;
It's not really clear to me what the data actually is here:
"
3-158-021"
Is there a carriage return/end of line character in your data?
This works for me
data want;
subject='"
3-158-021" ';
want = compress(subject,'"');
run;
From now on, please help us help you. Please don't say "it is not working" and provide no other information. Please, from now on, do provide the incorrect results that you are getting or the log with errors in it.
It's not really clear to me what the data actually is here:
"
3-158-021"
Is there a carriage return/end of line character in your data?
This works for me
data want;
subject='"
3-158-021" ';
want = compress(subject,'"');
run;
From now on, please help us help you. Please don't say "it is not working" and provide no other information. Please, from now on, do provide the incorrect results that you are getting or the log with errors in it.
It's because carriage return, I was not getting expected outcome. I tried with below code. Getting expected results. thank you.
subjectnumber=compress(Subject,'0D0A'x);
Hello @PC7,
Maybe it's easier to specify the characters to be kept in the subject ID, e.g., digits and hyphens (cf. the COMPRESS function documentation):
subjectnumber=compress(Subject,'-','kd');
Or display one of the strings in $HEX. format to find out exactly what characters need to be removed. Here's an example where those double quotes are in fact curly quotes:
data _null_; Subject='“3-158-021”'; put Subject $hex.; run;
Result in the log:
93332D3135382D30323194
So this would remove them:
subjectnumber=compress(Subject,'9394'x);
Excellent idea, @FreelanceReinh
If your compress function call
subjectnumber=strip(compress(Subject,'"'));
does not remove the character, then you specified the wrong character in the call.
So look at what character is actually in your data. Display it using the $HEX format. Once you find out what character it is then use a hex literal in the code. If you are running with ENCODING=UTF-8 and the character is one that uses multiple bytes (basically one that is not between a space and an tilde ) then use KCOMPRESS() instead.
Hi Ksharp,
I tried with DQUOTE function, still I was getting same results. I think it's because of carriage return. When I tried the below code, Geeting expected outcome.
subjectnumber=compress(Subject,'0D0A'x);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.