Hi,
I am trying to extract value after the '\'.
Example here is character UserID: CMS\jhi123, I would want to keep jhi123 only.
I wrote this:
data test;
User_Name =SCAN (UserID, -1, '\');
set test;
run;
But then I got this error: Variable UserID has been defined as both character and numeric. However UserID shows as character on SAS... Can anyone help me on this? (not sure if my code was correct too...)Thanks.
Order of operations matter. Since you are using Userid before the set statement and have not set any properties for the variable Userid SAS assumes it is numeric.
THEN you have the Set statement that brings another version of Userid in causing the conflict.
Moral of the story: If you want to use the value of a variable coming in on a SET statement the operation comes after the set statement.
Order of operations matter. Since you are using Userid before the set statement and have not set any properties for the variable Userid SAS assumes it is numeric.
THEN you have the Set statement that brings another version of Userid in causing the conflict.
Moral of the story: If you want to use the value of a variable coming in on a SET statement the operation comes after the set statement.
Thank you thank you! Didnt think about the order...
and I wrote this to make sure UserID is character:
data test;
set test(rename=(UserID=usernum));
UserID = put(usernum, 20.);
drop usernum;
run;
While syntactically correct you likely should not be using the
Data somename;
set somename;
code construct at this point in your experience. Unless there is a syntax error this will completely replace the Somename data set. A logic assignment error can cause serious issues.
I inherited code that looked like:
Data have;
set have;
x=x-1;
/* some other lines of code*/
end;
Guess what, the data set had a whole bunch of negative values. This was supposed to be changing a 1 to 4 code scheme to a 0 to 3.
However, the original coder had run the same code multiple times, likely while adding other lines of code. Replacing the starting data set every time and reducing X every time. I had to go back and find the original text file and re-read the data to actually use the data because the surviving version was too corrupt (this was not the only recode problem).
Better, at least until you are sure of a specific result to use
Data new;
set old;
However, this is not meant to imply that you need a new data set for each calculation. Test and then add the result to a previous block of code.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.