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

I have a file it is combination of some columns a,b,c,d,e .

but i imported the file and then created a column in sam1 

my output is like should be 

 

accounta1b1c1d1e1reson 
12542211452090889022a1|b1|c1|d1|e1
125985242157090 9022a1|b1||d1|e1
998745841757442045a1|b1|c1|d1|e1

 

Here my final required output is reason1 has

values in reson a1 & b1 are  'Old_Var'

 values in reson c1,d1 & e1 are 'New_Var'

if  reson in   a1 or b1 and c1 or d or e1 then it would be 'Old&New Var'

if reson any one in a1 or b1 then it would be 'Old Var' in to new variable.

Please help to create a variable by finding the reson values.

 

proc import datafile='C:\Users\HP\Desktop\datasamplecol.xls' out=sam dbms=xls
replace;
run;



data sam1;
set sam;
if a1 then a11='a1';
if b1 then b11='b1';
if c1 then c11='c1';
if d1 then d11='d1';
if e1 then e11='e1';
reson=compress(a11 || b11 || c11 || d11 || e11 ) ;

run;





proc sql;
create table sam2  as select *,
(case when reson contains ('a1') or reson contains ('b1') and reson contains ('c1') or reson contains ('d1') or reson contains ('e1')
then 'Old & New var' 
when reson contains ('c') or reson contains ('d') or reson contains ('e') then 'New Var' 
when reson contains ('a') or reson contains ('b')then 'Old Var' 
else ' ' end) as reason
from sam1;
run;



 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So did you try my code?  Looks like you might need change which variables are considered Old and which are New but it should work.

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Hi, @vinod4842 

 

Many of us will not download Microsoft Office documents, as they can be security threats. Please provide your data using these instructions: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

Also, please help us by going back and changing the Subject of your post to something meaningful.

--
Paige Miller
vinod4842
Fluorite | Level 6
accounta1b1c1d1e1
12542211452090889022
125985242157090 9022
998745841757442045
99877858778 5987 45
998745874257090 9022
998745841757   
99877858  598798745
99e77858  4398785
998745874257090   
99754158     
45745787   887 
778745 552   

Above one is data and my requirement is below like this 

and i am struggling

the create the corec_reason column

here a1,b1 are Old and C1,d1,e1 are New

if combination of any letter in reson column a1 or b1 becomes Old 

if any one values of c1 ,d1 ,e1 in reson becomes New

if any one value in  reson column contains  a1 or b1 and c1,d1,e1 becomes  Old & New 

Please help to create the column below dataset

 

accounta1b1c1d1e1reson corec_reason
12542211452090889022a1|b1|c1|d1|e1Old & New 
125985242157090 9022a1|b1||d1|e1Old & New 
998745841757442045a1|b1|c1|d1|e1Old & New 
99877858778 5987 45a1||c1||e1Old & New 
998745874257090 9022a1|b1||d1|e1Old & New 
998745841757   a1|b1|||Old & New 
99877858  598798745||c1|d1|e1New
99e77858  4398785||c1|d1|e1Old & New 
998745874257090   a1|b1|||Old & New 
99754158     ||||Old & New 
45745787   887 |||d1|New
778745 552   |b1|||Old
PaigeMiller
Diamond | Level 26

Again, I request the data be provided in the form requested

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

and I request you change the subject of this thread to a meaningful subject

--
Paige Miller
Tom
Super User Tom
Super User

So you want to test if any of A1,B1,C1 are non-missing and whether either of D1 or E1 is non-missing?

One way is to count the number of non-missing values using the N() function.

The CATX() function will let you build your resulting string from the two independent parts.

data want;
  set have ;
  length corec_reason $9;
  corec_reason = catx(' & '
    ,ifc(n(a1,b1,c1),'Old',' ')
    ,ifc(n(d1,e1),'New',' ')
  );
run;
vinod4842
Fluorite | Level 6
not missing & non missing values for corec_reason
here i need to generate old & new variables in other variable based on reson column
for example
here a1 and b1 are old variables
c1 d1 e1 are new variables
we need to show the old & new variable in another new variable that is corec_reason
if reson any of (a1 or b1 and c1 or d1 or e1) becomes combination of old & new variables
because a1 , b1 belongs to old variable and c1,d1,e1 belongs to new variable

if reson in a1 or 'b1' c1 or d1 or e1 then 'old & New'
if reson in a1 then 'Old'
if b1 and d1 then 'Old & new'
vinod4842
Fluorite | Level 6

Sorry it is a1 & b1 are old values and c1 d1 e1 are new values then 

for eg

reson                        Corec_reason

a1|b1|c1|d1|e1Old & New 
a1|b1||d1|e1Old & New 
a1|b1|c1|d1|e1Old & New 
a1||c1||e1Old & New 
a1|b1||d1|e1Old & New 
a1|b1|||old
||c1|d1|e1New
||c1|d1|e1New
a1|b1|||New
|||| 
|||d1|New
|b1|||Old

Please help team.

Tom
Super User Tom
Super User

Do you really need that column with the pipe characters? How does it add any value over the original columns?

vinod4842
Fluorite | Level 6

Ya working

 

Tom
Super User Tom
Super User

So did you try my code?  Looks like you might need change which variables are considered Old and which are New but it should work.

vinod4842
Fluorite | Level 6

Ya Its working thank you for your support

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 10 replies
  • 1399 views
  • 2 likes
  • 3 in conversation