BookmarkSubscribeRSS Feed
Feyng819
Obsidian | Level 7
If there is a number in var2, and there is a letter in var3, then I would like to add 4 rows of data, and remove the dash symbol in var2 if it appears, like the row of data “ 1 3 C ” I hope to convert it into:

1 . .
1 . C
3 . .
3 . C

If there is a number in var2 with no letter in var3, I would like to add 2 rows of data and remove the dash if it appears. Like the data “ 9 -10 .”,I hope to convert it into:

9 . .
10 . .

Otherwise the data will remain the same.

My data set:
Data have;
Input var1 $ var2 $ var3 $100.;
cards;
1 3 C
4 -5 B
7 . .
9 -10 .
12 . N
Run;

My expected output is:
var1 var2 var3
1 . .
1 . C
3 . .
3 . C
4 . .
4 . B
5 . .
5 . B
7 . .
9 . .
10 . .
12 . N

Thank you!
1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
  input VAR1 $ VAR2 $ VAR3 $;
cards;
1 3 C
4 -5 B
7 . .
9 -10 .
12 . N
run;

data WANT; 
  set HAVE;
  length V1-V3 $8;
  V1=VAR1; output;
  if VAR3 ne ' ' then do; 
    V3=VAR3; 
    output; 
  end;
  V3=' ';
  if VAR2 ne ' ' then do;
    V1=compress(VAR2,'-');
    output; 
    if VAR3 ne ' ' then do; 
      V3=VAR3; 
      output; 
    end;
  end;
  drop VAR1-VAR3;
  rename V1=VAR1 V2=VAR2 V3=VAR3;
run;
VAR1 VAR2 VAR3
1    
1   C
3    
3   C
4    
4   B
5    
5   B
7    
9    
10    
12    
12   N

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 446 views
  • 0 likes
  • 2 in conversation