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

I want to manipulate my existing dataset as below:

 

 

PartNumAltPartNumPrice PartNumPrice
998138.113 998138113
80901180568220➡️80901120
998204.165 80568220
    998204165

 

essentially, if there is an AltPartNum in an observation, I want to add the AltPartNum as PartNum in a new observation with the original price and basically drop the AltPartNum column altogether. want to do it in one data step or proc sql.

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

This assumes there are only2 vars to check

 


  data have;
  input PartNum	AltPartNum	Price;
  cards;
998138	.	113	 	998138	113
809011	805682	20	➡️	809011	20
998204	.	165
;

data want;
set have;
if n(PartNum,AltPartNum)=2 then do;
output;
PartNum=AltPartNum;
output;
end;
else if PartNum>. then output;
drop AltPartNum;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

This assumes there are only2 vars to check

 


  data have;
  input PartNum	AltPartNum	Price;
  cards;
998138	.	113	 	998138	113
809011	805682	20	➡️	809011	20
998204	.	165
;

data want;
set have;
if n(PartNum,AltPartNum)=2 then do;
output;
PartNum=AltPartNum;
output;
end;
else if PartNum>. then output;
drop AltPartNum;
run;
Astounding
PROC Star

It should be simple:

 

data want (drop=AltPartNum);

set have;

output;

if AltPartNum > .;

PartNum = AltPartNum;

output;

run;

 

The sample code assumes that the variables are numeric.  If they're character, the IF statement would read:

 

if AltPartNum > ' ';

PGStats
Opal | Level 21

You could use the missing() function to cover both numeric and character cases in a unified way.

PG
mkeintz
PROC Star

I would suggest the coalescec function, which returns the left most non-missing (non-blank in the case of character variables) argument from a list of arguments:

 

data want (drop=altpartnum);
  set have;
  partnum=coalesce(altpartnum,partnum);
run;

 

If PartNum and AltPartnum are numeric variables, then use the coalesce function instead.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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