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

Hi, 

I am trying to add new observations in my data set in an existing variable, but it does not seem to work. 

I have a variable called ID (identification number) and a variable called periodID (1-5), so I can add different periods per ID. Some ID's only have 1 periodID line where I would like to have 2 or 3, but I can't seem to add more lines (called periodID 2 and periodID 3). I am working from a pre-existing work.query. So I tried to make a new data set:

 

DATA newperiod;

SET oldperiod;

IF ID=200 THEN periodID=1, 2, 3;

RUN;

 

This gives me an error. Does somebody know how to fix this problem?

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
Ammonite | Level 13

It looks to me like you want to create a new table named newperiod which contains all of the original rows of data from oldperiod, but includes additional rows for ID 200. If that's the goal, you'll need to use a conditional DO loop with a explicit OUTPUT statements to control when rows get written to newperiod. Something like this should do the tirck:

/* Create some test data to play with */
data oldperiod; infile datalines truncover; input ID PeriodID; datalines; 100 1 100 2 200 300 1 300 2 300 3 400 1 400 2 500 1 ;
/* Add extra rows for ID # 200 */ data newperiod; set oldperiod; if ID=200 then do periodID=1, 2, 3; output; end; else output; run;

May the SAS be with you 🙂
Mark

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

2 REPLIES 2
SASJedi
Ammonite | Level 13

It looks to me like you want to create a new table named newperiod which contains all of the original rows of data from oldperiod, but includes additional rows for ID 200. If that's the goal, you'll need to use a conditional DO loop with a explicit OUTPUT statements to control when rows get written to newperiod. Something like this should do the tirck:

/* Create some test data to play with */
data oldperiod; infile datalines truncover; input ID PeriodID; datalines; 100 1 100 2 200 300 1 300 2 300 3 400 1 400 2 500 1 ;
/* Add extra rows for ID # 200 */ data newperiod; set oldperiod; if ID=200 then do periodID=1, 2, 3; output; end; else output; run;

May the SAS be with you 🙂
Mark

Check out my Jedi SAS Tricks for SAS Users
Qvika
Calcite | Level 5

Thank you so much Mark! that helped a lot.

But now when I try to add additional data my IF-THEN statement refuses to work. This is my code: 

DATA newperiod;

IF ID=200 then do periodID=1, 2, 3;
output;
end;
else IF ID=200 AND periodID=1 then do;

kidperiodstart=150;

kidperiodend=350;

end;
else output;

RUN;

 

The AND statement is not highlighted, how do I get this to work?