BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yichentian226
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

 

yichentian226
Obsidian | Level 7

Thank you thank you! Didnt think about the order... 

yichentian226
Obsidian | Level 7

and I wrote this to make sure UserID is character:

 

data test;

set test(rename=(UserID=usernum));

UserID = put(usernum, 20.);

drop usernum;

run;

ballardw
Super User

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.

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!

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
  • 4 replies
  • 562 views
  • 0 likes
  • 2 in conversation