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

Hi Team, 

 

I am trying to figure out  a way to solve the problem below. I have "Type" and "Viewname" and currently trying to find a way to get the "Needed" column. 

 

TypeViewnameNeeded
KIND-03prod.V_KIND-03_VISDTVISDT
KIND-03prod.V_KIND-03_NT_NINT_NI
MIND-03prod.V_MIND-03_LIDLID
FINDLE-03prod.V_FINDLE-03_LabLab

 

Thank you for your time. 

 

Have a pleasant day!!!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You didn't say so, but it appears that viewname always starts with "prod.V_" followed by type  followed by an underscore ("_"), followed by the value for needed.  If so, then concatenate "proc.V_" with type with an underscore and remove the resulting expression from viewname:

 

data have;
  input type :$9.  viewname :$20. ;
datalines;
KIND-03	prod.V_KIND-03_VISDT
KIND-03	prod.V_KIND-03_NT_NI
MIND-03	prod.V_MIND-03_LID
FINDLE-03	prod.V_FINDLE-03_Labrun
run;
data want;
  set have;
  *needed=transtrn(viewname,cats("prod.V_",type),'');  /*Corrected,see next line*/
  needed=transtrn(viewname,cats("prod.V_",type,'_'),'');
run;

  

Editted note: corrected code above.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

You didn't say so, but it appears that viewname always starts with "prod.V_" followed by type  followed by an underscore ("_"), followed by the value for needed.  If so, then concatenate "proc.V_" with type with an underscore and remove the resulting expression from viewname:

 

data have;
  input type :$9.  viewname :$20. ;
datalines;
KIND-03	prod.V_KIND-03_VISDT
KIND-03	prod.V_KIND-03_NT_NI
MIND-03	prod.V_MIND-03_LID
FINDLE-03	prod.V_FINDLE-03_Labrun
run;
data want;
  set have;
  *needed=transtrn(viewname,cats("prod.V_",type),'');  /*Corrected,see next line*/
  needed=transtrn(viewname,cats("prod.V_",type,'_'),'');
run;

  

Editted note: corrected code above.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

Or here some alternative syntax to achieve the same.

data want;
  set have;
  needed=substr(viewname,find(viewname,strip(type),'i')+length(type)+1);
run;
shasank
Quartz | Level 8
Thank you so much!!. This worked too. Very helpful.
PaigeMiller
Diamond | Level 26

Could you please state the rules that should be followed to extract this string? You know your data, we don't, so we'd have to guess, guessing is not a good way for us to proceed, and its hard to generalize from these four examples.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2497 views
  • 1 like
  • 4 in conversation