BookmarkSubscribeRSS Feed
WAL83
Obsidian | Level 7
I have a dataset similar to:

age frequency
20 2
21 3
23 5
24 3

and I want to create a new dataset from the above dataset that looks like:

age frequency
20 1
20 1
21 1
21 1
21 1
23 1
23 1
23 1
23 1
23 1
24 1
24 1
24 1

How can I accomplish this. Thanks.
3 REPLIES 3
Cynthia_sas
SAS Super FREQ
Hi:
Inside a DATA step program, you would need to use a DO loop. If you want the value of the FREQUENCY variable to change to 1, then you'd probably need to save the original value within your DATA step logic. Something like this untested code shown below.

cynthia
[pre]
DATA new;
. . . more code to read data either with INFILE/INPUT or SET statement . . .
. . . may also want KEEP or DROP statement here . . .

** grab original frequency value into new variable name;
orig_frequency = frequency;

** set frequency value to 1;
frequency=1;

** Use original frequency value in do loop for output;
** control output in DO loop -- one observation will be output;
** for every iteration of the DO loop.;
do i = 1 to orig_frequency;
output new;
end;
. . . more code to end program . . .
run;
[/pre]
WAL83
Obsidian | Level 7
Thanks Cynthia. I'll give it a shot.
WAL83
Obsidian | Level 7
Thanks Cynthia. It worked perfectly 🙂

proc import out=data
datafile= "c:\file.xlsx"
dbms=excel replace;
range="Sheet1$";
getname=yes;
mixed=no;
scantext=yes;
usedate=yes;
scantime=yes;
run;

data data1;
set data;
orig_frequency=frequency;
frequency=1;
do i = 1 to orig_frequency;
output data1;
end;
run;

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
  • 3 replies
  • 985 views
  • 0 likes
  • 2 in conversation