BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All

  This is the code that I have written.  Am I making a mistake?

 

Data A B  ; Set have;

If VarA = 1 then output A (Drop = VarF (Rename = (VarM = VarMA VarN= VarNA)));

If VarA = 2 then output B (Drop = VarF (Rename = (VarK = VarKA VarL= VarLA)));

run;

 

 

5 REPLIES 5
Reeza
Super User
There is a difference between your code being correct syntax wise, versus logically correct. From a syntax perspective your code appears correct but we have no way of knowing if it's what you're expecting. Or even if it's correct because of the variable names in the original data set are unknown.

So we cannot tell you if you're making a mistake. What are you trying to do?
RandyStan
Fluorite | Level 6

I am trying to split the data set "have" into different data sets and rename variables.  I can use multiple data steps; but for efficiency I just wanted to use one data statement.

 

When I run the code; I get an error statement on the "DROP" part of the code

PaigeMiller
Diamond | Level 26

@RandyStan wrote:

 

When I run the code; I get an error statement on the "DROP" part of the code


Why withhold this information from us in your original post? Anyway, @Tom has explained what the error is.

 

But rarely do you need to split a data set like this. You could use a BY statement in whatever analyses you are doing, and then the splitting of the data set can be altogether avoided.

 

Even if you want to do separate analyses when VarA=1 or VarA=2, one data set suffices, and so you may be performing extra unnecessary work, and if this is a large dataset, splitting it like this takes up more storage and more execution time (well, that's also true if it is a small data set, but then you won't really notice the extra execution time). Example:

 

proc means data=have(where=(vara=1) Drop = VarF Rename = (VarM = VarMA VarN= VarNA));
    /* more PROC MEANS statements */
run;

 

--
Paige Miller
PaigeMiller
Diamond | Level 26

@RandyStan wrote:

 

  This is the code that I have written.  Am I making a mistake?


Did you run the code? Did it work? Did you do what you want? How can we know if you are making a mistake?

 

Also, I have never seen this exact syntax (it may work, but I think it's wrong unless you remove the parentheses that I have colored red

 

(Drop = VarF (Rename = (VarM = VarMA VarN= VarNA)))

--
Paige Miller
Tom
Super User Tom
Super User

No.  You cannot have dataset options on an OUTPUT statement. Those belong where the output dataset is defined.

data 
  A (Drop = VarF Rename = (VarM = VarMA VarN= VarNA))
  B (Drop = VarF Rename = (VarK = VarKA VarL= VarLA))
;
  set have;
  if VarA = 1 then output A;
  if VarA = 2 then output B;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 1107 views
  • 7 likes
  • 4 in conversation