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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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