BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mscarboncopy
Pyrite | Level 9
I need to make the keys from var  B   become the continuation of var  A   
The original var  A   has  values from 1-22 and once I rename B into A  it would go up to 51 (as you can see in the code below).
 
My code works except it looks like I am losing only the first prep var so I might need only one statement somewhere to prevent it from dropping that first one? 
Please see the data example after the code.
 
Data want;
set have;

if B   = 1 then prepvar = 23 ;

else if B   = 2 then prepvar = 24 ;

else if B   = 3 then prepvar = 25 ;

else if B   = 4 then prepvar = 26 ;

else if B   = 5 then prepvar = 27 ;

else if B   = 6 then prepvar = 28 ;

else if B   = 7 then prepvar = 29 ;

else if B   = 8 then prepvar = 30 ;

else if B   = 9 then prepvar = 31 ;

else if B   = 10 then prepvar = 32 ;

else if B   = 11 then prepvar = 33 ;

else if B   = 12 then prepvar = 34 ;

else if B   = 13 then prepvar = 35 ;

else if B   = 14 then prepvar = 36 ;

else if B   = 15 then prepvar = 37 ;

else if B   = 16 then prepvar = 38 ;

else if B   = 17 then prepvar = 39 ;

else if B   = 18 then prepvar = 40 ;

else if B   = 19 then prepvar = 41 ;

else if B   = 20 then prepvar = 42 ;

else if B   = 21 then prepvar = 43 ;

else if B   = 22 then prepvar = 44 ;

else if B   = 23 then prepvar = 45 ;

else if B   = 24 then prepvar = 46 ;

else if B   = 25 then prepvar = 47 ;

else if B   = 26 then prepvar = 48 ;

else if B   = 27 then prepvar = 49 ;

else if B   = 28 then prepvar = 50 ;

else if B   = 29 then prepvar = 51 ;

if 1<=A<=22 then A = A;  *keeping the var as is for the original data that has values that go from 1-2;

else A= prepvar;  * adding the new values from var B;
Run;
 
Example of what I am getting. it looks like it is dropping the first prepvar value only. I am not sure why and how to prevent this from  happening.
ID                            VAR A               VAR B   prepvar
Mscarboncopy_0-1623260162872.png

 

Thanks,
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

This works for what you've posted:

 

data have;
infile cards dsd;
input ID $    A B     Prep;
cards;
A,          6,                   5 ,           27
A,          . ,                   6 ,           28
A,          . ,                   11 ,         33
A,          . ,                   12  ,        34
A,          . ,                   22  ,        44
A,          . ,                   24  ,        46
A,          . ,                   25  ,        47
A,          . ,                   29  ,        51
;;;;
run;

data want;
set have (in=t1 keep=  ID A where=(not missing(A))) 
    have(in=t2 drop = A rename= B=A);
if t2 then A = A + 22;  
run;

View solution in original post

10 REPLIES 10
Reeza
Super User

It's hard to see what you're doing versus what you want. 

Can you show us what you start with exactly and what you want to see as the end results. 

 

You code can be simplified significantly, but if it's not working, that may not be very helpful.

 

Right now your logic is :

  • If Variable B is between the values of 1 and 29 create a new variable PrepVar which adds 22 to the value of B.
  • If A is not between 1 and 22 then re-assign the value of prepvar to the variable A

 

data want;
set have;

if B>=1  and B<=29 then prepVar = B+22;

*If VarA not a value in 1 to 22, replace it with prepvar;
if  not (A >=1 and A <= 22) then A = prepvar;

run;
Spoiler

@Mscarboncopy wrote:
I need to make the keys from var  B   become the continuation of var  A   
The original var  A   has  values from 1-22 and once I rename B into A  it would go up to 51 (as you can see in the code below).
 
My code works except it looks like I am losing only the first prep var so I might need only one statement somewhere to prevent it from dropping that first one? 
Please see the data example after the code.
 
Data want;
set have;

if B   = 1 then prepvar = 23 ;

else if B   = 2 then prepvar = 24 ;

else if B   = 3 then prepvar = 25 ;

else if B   = 4 then prepvar = 26 ;

else if B   = 5 then prepvar = 27 ;

else if B   = 6 then prepvar = 28 ;

else if B   = 7 then prepvar = 29 ;

else if B   = 8 then prepvar = 30 ;

else if B   = 9 then prepvar = 31 ;

else if B   = 10 then prepvar = 32 ;

else if B   = 11 then prepvar = 33 ;

else if B   = 12 then prepvar = 34 ;

else if B   = 13 then prepvar = 35 ;

else if B   = 14 then prepvar = 36 ;

else if B   = 15 then prepvar = 37 ;

else if B   = 16 then prepvar = 38 ;

else if B   = 17 then prepvar = 39 ;

else if B   = 18 then prepvar = 40 ;

else if B   = 19 then prepvar = 41 ;

else if B   = 20 then prepvar = 42 ;

else if B   = 21 then prepvar = 43 ;

else if B   = 22 then prepvar = 44 ;

else if B   = 23 then prepvar = 45 ;

else if B   = 24 then prepvar = 46 ;

else if B   = 25 then prepvar = 47 ;

else if B   = 26 then prepvar = 48 ;

else if B   = 27 then prepvar = 49 ;

else if B   = 28 then prepvar = 50 ;

else if B   = 29 then prepvar = 51 ;

if 1<=A<=22 then A = A;  *keeping the var as is for the original data that has values that go from 1-2;

else A= prepvar;  * adding the new values from var B;
Run;
 
Example of what I am getting. it looks like it is dropping the first prepvar value only. I am not sure why and how to prevent this from  happening.
ID                            VAR A               VAR B   prepvar
Mscarboncopy_0-1623260162872.png

 

Thanks,

Mscarboncopy
Pyrite | Level 9

Thank you. This is a much more elegant way to do what I was doing. It is still dropping the first prep var though.

Reeza
Super User
It's hard to see what you're doing versus what you want.

Can you show us what you start with exactly and what you want to see as the end results. Since your code is not working I have no idea what you mean by dropping the first prep_var. Please be more specific.
Mscarboncopy
Pyrite | Level 9

I don't know what else to show. It is highlighted in yellow in the data example I gave. The example is showing the 2 vars A and B and prep. I am not  doing anything else other than trying to combine A and B using the prep var. And somehow, for each id, the combining is dropping the first prep var. It works for all the others but never the first one. Those are simply dropped. I should have 9 entries for ID A for example and I end up with only 8 because the first  prep var  (in this case 27) disappears. It is not in VAR A, as you can see. Thank you.

Reeza
Super User
I should have 9 entries for ID A for example and I end up with only 8 because the first prep var (in this case 27) disappears. It is not in VAR A, as you can see.

Actually I can't see what's not there and I don't even know you want it there.
How can I know you wanted to add rows - this is the first time you've mentioned that?

Please show your input.
Please show what you expect as the output, preferably that aligns with the output. For example, it should have the missing row you mentioned.
Mscarboncopy
Pyrite | Level 9

Oh I see. Sorry about that. This is the input for A: 

          VAR A          VAR B     Prep

A          6                   5            27

A          .                    6            28

A          .                    11          33

A          .                    12          34

A          .                    22          44

A          .                    24          46

A          .                    25          47

A          .                    29          51

 

Reeza
Super User
And the expected output please?
Mscarboncopy
Pyrite | Level 9

ID    VAR  A

A         6
A        27  
A        28
A        33
A        34
A        44
A        46
A        47
A        51

Reeza
Super User

This works for what you've posted:

 

data have;
infile cards dsd;
input ID $    A B     Prep;
cards;
A,          6,                   5 ,           27
A,          . ,                   6 ,           28
A,          . ,                   11 ,         33
A,          . ,                   12  ,        34
A,          . ,                   22  ,        44
A,          . ,                   24  ,        46
A,          . ,                   25  ,        47
A,          . ,                   29  ,        51
;;;;
run;

data want;
set have (in=t1 keep=  ID A where=(not missing(A))) 
    have(in=t2 drop = A rename= B=A);
if t2 then A = A + 22;  
run;
Mscarboncopy
Pyrite | Level 9

That is it. Thank you !!

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 668 views
  • 0 likes
  • 2 in conversation