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

Okk. Got it, Thanks a lot.

Tom
Super User Tom
Super User

So you want to roll the values up into an array.   To do what you want you could use this simple data step.  Nesting the SET statement inside the DO loop eliminates the need to RETAIN the variables or add an explicit OUTPUT statement to keep only one observation per state.

data want ;

  do until (last.state) ;

    set dl.short_state_soc22;

    by state;

    array occ occ_01 - occ_053 ;

    if 1<= soc_22 <= dim(occ) then occ(soc_22) = count;

  end;

  keep state occ_11 occ_13 occ_15 occ_17 occ_19 occ_21 occ_23

       occ_25 occ_27 occ_29 occ_31 occ_33 occ_35 occ_37 occ_39 occ_41

       occ_43 occ_45 occ_47 occ_49 occ_51 occ_53

  ;

run;

If you want the columns that do not have counts to be zero instead of missing then you will need to add code to set them to zero.  One way is to add another do loop after the first one.

do _n_ = 1 to dim(occ);

occ(_n_) = sum(occ(_n_),0);

end;

I am not sure why are you only want to keep the odd numbers between 11 and 53?  If it is because those are the only values in your data then you could make that decision dynamic by using SQL to query the data and generate the list to keep. It could also determine the upper bound on your array.

You could query the data to get the maximum value of SOC_22 and the distinct list of values that are present.

proc sql noprint ;

  select max(soc_22) into :max separated by ' '

    from dl.short_state_soc22

  ;

  select distinct catx('_','occ',soc_22)

    into :keeplist separated by ' '

    from dl.short_state_soc22

  ;

quit;


data want ;

  do until (last.state) ;

    set dl.short_state_soc22;

    by state;

    array occ occ_01 - occ_&max;

    if 1<= soc_22 <= dim(occ) then occ(soc_22) = count;

  end;

  keep state &keeplist ;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 16 replies
  • 2019 views
  • 9 likes
  • 4 in conversation