BookmarkSubscribeRSS Feed
lerdem
Quartz | Level 8

Data set i have is as below:

       var1  var2    var3  var4

       A      -        32     12

       B      -        25      14

        -       C       -       18

       -        D       15     13

i need to combine var1 and var2

  var12   var3   var4

     A       32      12

    B        25       14

    C        -         18

   D        15        13

5 REPLIES 5
Steelers_In_DC
Barite | Level 11

I'm assuming there is a lot more that goes along with this.  Here is a solution for you, but based on any other nuances to the data or size of data there is a chance there is a much better solution:

data have;

infile cards dsd;

input var1$  var2$    var3  var4;

cards;

A,,32,12

B,,25,14

,C,,18

,D,15,13

;

run;

data want;

set have;

if not missing(var1) then var12 = var1;

if not missing(var2) then var12 = var2;

drop var1 var2;

run;

CurlerBob
Fluorite | Level 6

I'd use a pretty simple proc sql.  The coalesce function will take the first non-missing value.  If the "-" is actually "" in your data, the following will work.

proc sql;

     create table NewDataset as

     select coalesce(var1, var2) as var12,

          var3,

          var4

     from OriginalDataset;

quit;

if the field is actually populated with "-" then a simple reassignment statement will work:

     if var1 = '-' then var12 = var1;

          else var12 = var2;


RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

data want (drop=var1);

     set have;

     var2=coalesce(var1,var2);

run;

Dalveer
Calcite | Level 5

try this below code: give you desired output:-->

data need;

set have;

if var1 = '-' then var12 = var2;

else var12=var1;

run;

MadhuKorni
Quartz | Level 8

data want;

set have;

var12=compress(catt(var1,var2),'-'); *If you want to remove only '-' after combining;

var11=compress(catt(var1,var2),,'ka'); *If you want to delete any special characters and digits after combining;

drop var1 var2;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 1386 views
  • 1 like
  • 6 in conversation