Write and run SAS programs in your web browser

Problem creating new variables while keeping existing variables

Accepted Solution Solved
Reply
Occasional Contributor LMP
Occasional Contributor
Posts: 12
Accepted Solution

Problem creating new variables while keeping existing variables

Hello,

 

   I have been trying to create new variables in a dataset that I exported into SAS studio UE. I have tried this multiple times. Everytime, I try to create new variables it deletes the existing variables, and there are blanks in the columns of the new variables I tried to create. I have been trying to create new variables regarding age at a particular event. I have tried this multiple ways; however, I still end up getting a period( (.) which means blank in SAS)  in the one row for each variable. Also, none of the previous dataset information appears in the output after running this step; thus, I tried to add a Keep var  statement.  Prior to running this step I run the Libname and DATA set statement. I provided screen shots of my last coding attempt.

 

Can any one assist or give any kind of advice?  If so, I would greatly appreciate it!

 

Thanks,

LMP

 

 

Capture 1 sas code.PNGCapture 2 sas.PNGCapture 3 SAS code.PNG


Accepted Solutions
Solution
‎03-02-2017 03:43 PM
Grand Advisor
Posts: 10,225

Re: Problem creating new variables while keeping existing variables

1) I don't see a SET statement in your code on the second data step. So instead of bringing data in from an existing set you are creating a new one with all missing values for the variables in the KEEP statements.. Try:

data mydata.temp;
   set mydata.bllcinckids2014;

and add you calculations to that data step.

 

2) Your syntax for YRDIF is incorrect. You likely want age = yrdif(dob, sampledate,'ACT/ACT'); the Earlier date should be referenced first and YRDIF requires the basis argument of ACT/ACT or the 360 365 variants. Your code should have thrown many error messages.

 

3) ALL variables are kept by default. Drop may be easier to use. Note that you can use variable lists to shorten the code:

Keep Result: ; would keep all of the variables whose names start with Result (note that there is a : at the end)

or

Drop Result: ; would remove all of the result variables.

 

4) Many times when there are so many variables with indexed names such as Result, Test Sampledate and so on it indicates that the data structure may be incorrect.

 

5) Post LOG results when you get errors. And copy/paste Code using the forum {i} menu icon. Noone is going to retype code from a picture to test the results.

View solution in original post


All Replies
Solution
‎03-02-2017 03:43 PM
Grand Advisor
Posts: 10,225

Re: Problem creating new variables while keeping existing variables

1) I don't see a SET statement in your code on the second data step. So instead of bringing data in from an existing set you are creating a new one with all missing values for the variables in the KEEP statements.. Try:

data mydata.temp;
   set mydata.bllcinckids2014;

and add you calculations to that data step.

 

2) Your syntax for YRDIF is incorrect. You likely want age = yrdif(dob, sampledate,'ACT/ACT'); the Earlier date should be referenced first and YRDIF requires the basis argument of ACT/ACT or the 360 365 variants. Your code should have thrown many error messages.

 

3) ALL variables are kept by default. Drop may be easier to use. Note that you can use variable lists to shorten the code:

Keep Result: ; would keep all of the variables whose names start with Result (note that there is a : at the end)

or

Drop Result: ; would remove all of the result variables.

 

4) Many times when there are so many variables with indexed names such as Result, Test Sampledate and so on it indicates that the data structure may be incorrect.

 

5) Post LOG results when you get errors. And copy/paste Code using the forum {i} menu icon. Noone is going to retype code from a picture to test the results.

Occasional Contributor LMP
Occasional Contributor
Posts: 12

Re: Problem creating new variables while keeping existing variables

Thank you! I will try this out!
Occasional Contributor LMP
Occasional Contributor
Posts: 12

Re: Problem creating new variables while keeping existing variables

Thank you so much ! It worked! Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 105 views
  • 0 likes
  • 2 in conversation