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
SAS Super FREQ

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
SAS Super FREQ

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?

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 456 views
  • 1 like
  • 2 in conversation