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

I have this dataset

 

 and I want to create a new variable based on C3 and C3_F.

When C3 has a value I want this value. When not, like the green example shown, I want the C3_F value.

Can you help me please?

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The link works for me.

Here how this could look like in a data step. Make sure to define the length of new_var long enough to hold the string (=the greater length of your two source variables)

length new_var $20;
new_var=coalescec(cae3,cae3_fue);

View solution in original post

8 REPLIES 8
Patrick
Opal | Level 21

You could use the coalescec() function documented here

ramgouveia
Obsidian | Level 7
Patrick

The link doesn't work.
Patrick
Opal | Level 21

The link works for me.

Here how this could look like in a data step. Make sure to define the length of new_var long enough to hold the string (=the greater length of your two source variables)

length new_var $20;
new_var=coalescec(cae3,cae3_fue);
andreas_lds
Jade | Level 19

@Patrick the link does not work:

 

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Apr 07 12:26:11 UTC 2021
There was an unexpected error (type=Not Found, status=404).
ramgouveia
Obsidian | Level 7

Thank you very much @Patrick !

It worked!

I used this code:

DATA D;
set D;
length C3_E $20.;
C3_E=coalescec(c3,c3_f);
run;

Patrick
Opal | Level 21

Don't just use the length of $20 from my sample but chose a length which matches your source variables. If the length is too short then you will truncate the string, if it's too long then you're wasting space.

You can run below script. Then look-up in the report what lengths your two source variables have and use the bigger of the two values in the length statement for your new variable.

proc contents data=DEM_MES.MERGEEFATURADMRSS202001;
run;quit;
Kurt_Bremser
Super User

I took the liberty to update the link, it should work now everywhere.

(I just copy/pasted the link that I get from the documentation website, I have no clue what was wrong)

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
  • 8 replies
  • 1035 views
  • 7 likes
  • 4 in conversation