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

 

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
  • 1 reply
  • 273 views
  • 0 likes
  • 2 in conversation